【问题标题】:Youtube pause all iframes with same classYoutube 暂停所有具有相同类的 iframe
【发布时间】:2013-01-12 06:12:38
【问题描述】:

我想通过简单的点击暂停所有 youtube 电影(与其 ID 无关)。我为可以使用特定 ID 的代码制作了一个 jsfiddle。但是,由于我无法分配唯一 ID,因此我希望能够暂停所有 youtube iframe。然而,iframe 确实有一个类。 http://jsfiddle.net/4bPAJ/3/ 感谢您在正确方向上的任何帮助:)

干杯, 阿德南

Javascript:

function callPlayer(e, t, n) {
    function o(e, t) {
        var n = e ? window.addEventListener : window.removeEventListener;
        n ? n("message", t, !1) : (e ? window.attachEvent : window.detachEvent)("onmessage", t)
    }
    if (window.jQuery && e instanceof jQuery) e = e.get(0).id;
    var r = document.getElementById(e);
    if (r && r.tagName.toUpperCase() != "IFRAME") {
        r = r.getElementsByTagName("iframe")[0]
    }
    if (!callPlayer.queue) callPlayer.queue = {};
    var i = callPlayer.queue[e],
        s = document.readyState == "complete";
    if (s && !r) {
        window.console && console.log("callPlayer: Frame not found; id=" + e);
        if (i) clearInterval(i.poller)
    } else if (t === "listening") {
        if (r && r.contentWindow) {
            t = '{"event":"listening","id":' + JSON.stringify("" + e) + "}";
            r.contentWindow.postMessage(t, "*")
        }
    } else if (!s || r && (!r.contentWindow || i && !i.ready)) {
        if (!i) i = callPlayer.queue[e] = [];
        i.push([t, n]);
        if (!("poller" in i)) {
            i.poller = setInterval(function () {
                callPlayer(e, "listening")
            }, 250);
            o(1, function u(t) {
                var n = JSON.parse(t.data);
                if (n && n.id == e && n.event == "onReady") {
                    clearInterval(i.poller);
                    i.ready = true;
                    o(0, u);
                    while (n = i.shift()) {
                        callPlayer(e, n[0], n[1])
                    }
                }
            }, false)
        }
    } else if (r && r.contentWindow) {
        if (t.call) return t();
        r.contentWindow.postMessage(JSON.stringify({
            event: "command",
            func: t,
            args: n || [],
            id: e
        }), "*")
    }
}
callPlayer("whateverID", "playVideo")

【问题讨论】:

    标签: jquery iframe youtube-api


    【解决方案1】:

    试试这个:

    $(document).ready(function () {
        var i = 0;
        $(".youtubeplayer").each(function () {
            $(this).attr("id", "youtubeplayer" + i);
            callPlayer("youtubeplayer" + i, "playVideo");
            i++;
        });
    });
    

    这将为每一帧添加一个id,并在后面调用函数。

    【讨论】:

    • 谢谢:)。 vimeo API 比 youtube API 好得多,尽管 this.contentWindow.postMessage('{ "method": "pause" }', "player.vimeo.com"); 就足够了
    猜你喜欢
    • 2015-02-13
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 2017-04-11
    • 2012-11-05
    • 2012-10-06
    • 1970-01-01
    相关资源
    最近更新 更多