首页 > 解决方案 > 更改动态创建的 iframe 的 src

问题描述

我想创建一个输入字段来更改 js 内部的 iframe 我是 javascript 和整体编程的中级人员,我想要一些帮助将输入字段事件绑定到 iframe src,我希望它只更改 s=$ {?}, e=${?} 请记住,这是在覆盖层内发生的,需要发生事件才能使 iframe 显示出来,所以我不希望它重新加载

function openNav(show) {
    let id = show.id;
    fetch(BASE_URL + '/tv/' + id + '/videos?' + API_KEY).then(res => res.json()).then(videoData => {
        console.log(videoData);
        if (videoData) {
            document.getElementById("myNav").style.width = "100%";
            if (videoData.results.length > 0) {
                var embed = [];
                var links = [];
                videoData.results.forEach((video) => {
                    let { name, key, site } = video

                    if (site == 'YouTube') {

                        embed.push(`
                <iframe id="tvshow" width= "1099px" height= "618px" src="https://www.2embed.ru/embed/tmdb/tv?id=${id}&s=1&e=1" class="embed hide" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> `)
                    }


                })

    Inputfield.addEventListener("submit", (e) => {
    const searchInput = search.value;
    if (searchInput) {
        tvshow.setAttribute("src", searchInput)
    }
})
<form id="Inputfield">
            <label for="Episode">Episode</label><br>
            <input type="text" name="Episode"><br>
            
          </form>

标签: javascripthtmleventsiframewebapi

解决方案


推荐阅读