Stop all YouTube iframe embeds from playing?

前端 未结 1 353
名媛妹妹
名媛妹妹 2021-01-12 00:47

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 incremen

相关标签:
1条回答
  • 2021-01-12 01:53

    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');
    
    0 讨论(0)
提交回复
热议问题