停止嵌入 youtube iframe?

2022-01-19 00:00:00 youtube-api iframe javascript html

我正在使用 YouTube iframe 在我的网站上嵌入视频.

I'm using YouTube iframe to embed videos on my site.

<iframe width="100%" height="443" class="yvideo" id="p1QgNF6J1h0"
              src="http://www.youtube.com/embed/p1QgNF6J1h0?rel=0&controls=0&hd=1&showinfo=0&enablejsapi=1"
              frameborder="0" allowfullscreen>
</iframe>

我在同一页面上有多个视频.

And i have multiplie videos on the same page.

我想使用 javascript 左右单击一个按钮来停止所有这些或其中一个.

I want to stop all of them or one of them in a click of a button using javascript or so.

有可能吗?

我尝试了 Talvi Watia 所说和使用的方法:

I tried what Talvi Watia said and used:

$('#myStopClickButton').click(function(){
  $('.yvideo').each(function(){
    $(this).stopVideo();
  });
});

我明白了:

Uncaught TypeError: Object [object Object] has no method 'stopVideo' 

推荐答案

您可能需要通过 Youtube JavaScript API 进行审核 参考文档.

You may want to review through the Youtube JavaScript API Reference docs.

当您在页面上嵌入视频时,您需要传递此参数:

When you embed your video(s) on the page, you will need to pass this parameter:

http://www.youtube.com/v/VIDEO_ID?version=3&enablejsapi=1

如果您想要停止所有视频按钮,您可以设置一个 javascript 例程来循环播放视频并停止它们:

If you want a stop all videos button, you can setup a javascript routine to loop through your videos and stop them:

player.stopVideo()

这确实涉及跟踪页面上每个视频的所有页面 ID.更简单的可能是创建一个类,然后使用 jQuery.each.

This does involve keeping track of all the page IDs for each video on the page. Even simpler might be to make a class and then use jQuery.each.

$('#myStopClickButton').click(function(){
  $('.myVideoClass').each(function(){
    $(this).stopVideo();
  });
});

相关文章