javascript 停止播放所有 YouTube iframe 嵌入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5503833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stop all YouTube iframe embeds from playing?
提问by RustyEight
I'm stuck! I'm using jquery to, on click, pull a youtube json-c feed, grab the first id, and use it to prepend the html5 iframe video to the top of the ul. I then increment it and pull the second id, third id, etc and prepend them accordingly.
我被卡住了!我正在使用 jquery,单击时拉取 youtube json-c 提要,获取第一个 id,并使用它将 html5 iframe 视频添加到 ul 的顶部。然后我增加它并拉出第二个 id、第三个 id 等,并相应地添加它们。
The problem is, when a new video is prepended, I want any of the previous videos to stop playing. Is their any way to accomplish this with javascript/jquery?
问题是,当添加新视频时,我希望之前的任何视频都停止播放。他们有什么办法可以用 javascript/jquery 来完成这个吗?
Here's the function call:
这是函数调用:
function videoCall(i) {
$.getJSON('http://gdata.youtube.com/feeds/api/standardfeeds/most_recent?v=2&max-results=50&alt=jsonc', function(vid) {
var fullItem ='';
var videoID = (vid.data.items[i].id);
var videoLink = 'http://www.youtube.com/watch?v=' + videoID;
var videoTitle = (vid.data.items[i].title);
fullItem += '<li class="videoItem">';
fullItem += '<iframe title="YouTube video player" width="900" height="536" src="http://www.youtube.com/embed/' + videoID + '" frameborder="0" allowfullscreen></iframe>';
fullItem += '<a href="mailto:?subject=ThinkerBot Video&body=' + videoLink + '">Email This</a>';
fullItem += '</li>';
$(fullItem).hide().prependTo('#content ul').slideDown('slow');
});
}
回答by Kim T
You should be able to do something like this:
你应该能够做这样的事情:
function players(func, args) {
var iframes = document.getElementsByTagName('iframe');
for (var i=0; i<iframes.length; ++i) {
if (iframes[i]) {
var src = iframes[i].getAttribute('src');
if (src) {
if (src.indexOf('youtube.com/embed') != -1) {
iframes[i].contentWindow.postMessage(JSON.stringify({'event': 'command','func': func,'args': args || []}), "*");
}
}
}
}
}
// example usage
players('stopVideo');