【问题标题】:Append Iframe src附加 iframe src
【发布时间】:2020-09-02 08:43:47
【问题描述】:

我有一个 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');
});

【问题讨论】:

  • 错误是什么?如果您不告诉我们错误是什么,我们将无法帮助解决错误:)
  • 糟糕!未捕获的类型错误:$(...).parent(...).parent(...).parent(...).find(...).attr(...).appendTo 不是函数
  • 这是因为attr('src')返回一个字符串。你不能在字符串上调用appendTo()

标签: html jquery


【解决方案1】:

要设置 jQuery 对象的src 属性,您应该改用.attr() method,即:.attr('src', &lt;SOURCE&gt;):

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-26
    • 2023-03-28
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 2019-01-07
    相关资源
    最近更新 更多