首页 > 解决方案 > 附加 iframe src

问题描述

我有一个 onclick 函数,我希望 iframe 附加到我网站中的另一个 div。因此,当用户单击“watch-youtube-video”时,它将从“youtube-video”中获取 iframe,并将 src 附加到“fixed-player”iframe src。

我曾尝试使用下面的 jQuery,但我遇到了错误。有人可以帮忙吗?

HTML

<div class="fixed-player"><iframe src=""></div>

<div class="span-row wow scale-anm all animated">

    <div class="portal-img ">

        <div class="youtube-video">
            <iframe frameborder="0" height="100%" width="100%" src="https://youtube.com/embed/10zsTgv8MxA?autoplay=1&amp;mute=1&amp;controls=0&amp;showinfo=0&amp;autohide=1&amp;loop=1" allowautoplay="" allowfullscreen="" data-origwidth="100%" data-origheight="100%" style="width: 0px;">
            </iframe>
        </div>           


    </div>

<div class="portal-cont ">

  <div class="top-cont ">

    <div class="main-button grey-btn watch-youtube-video">
      Watch Video
    </div>           

  </div>
</div>

</div>

jQuery

$('.watch-youtube-video').on('click', function(){
    $('.overlay').fadeIn();
    $('.youtube-viewer-pop').fadeIn();
    $(this).parent().parent().parent().find("iframe").attr("src").appendTo('fixed-player');
});

标签: htmljquery

解决方案


要设置srcjQuery 对象的属性,您应该改用该.attr()方法,即.attr('src', <SOURCE>:):

const src =  $(this).parent().parent().parent().find("iframe")[0].src;
$('.fixed-player').attr('src', src);

推荐阅读