多个youtube iframe播放此内容

Multiple youtube iframes play play this

本文关键字:播放 youtube iframe 多个      更新时间:2023-09-26

我有这个代码:

<a id="play-video" href="#">Play Video</a><br />
<iframe id="video" width="420" height="315" src="//www.youtube.com/embed/9B7te184ZpQ?rel=0" frameborder="0" allowfullscreen></iframe>

然后这个播放视频的jquery:

$(document).ready(function() {
  $('#play-video').on('click', function(ev) {
    $("#video")[0].src += "&autoplay=1";
    ev.preventDefault();
  });
});

如何修改jquery:

$("#video")[0].src += "&autoplay=1";

所以它只播放我有多个iframe的选定内容?

您可以将视频id更改为其他id。例如#video1,#video来控制你想要播放的视频,或者将按钮和视频包装在div中。然后将代码更改为

<div>
<a id="play-video" href="#">Play Video</a><br />
<iframe id="video" width="420" height="315" src="//www.youtube.com/embed/9B7te184ZpQ?rel=0" frameborder="0" allowfullscreen></iframe>
</div>

这个

$().ready(function () {
    $('.play-video').click(function () {
    //alert($(this).parent().children('#video').attr(src));
        $(this).parent().children('#video').attr('src', function () {
        return this + "&autoplay=1";
    });
  });
});

Demohttps://jsfiddle.net/minhthanh/acg8e7f1/