4

所以我的页面上有一个 iframe

<iframe width="640" height="360" frameborder="0" allowfullscreen="" src="http://www.youtube.com/xxxxxx">

我想知道如何在此捕获点击事件。Iframe 上没有设置 id 或类

4

2 回答 2

7

我遇到了一种情况,我必须跟踪通过 iframe 拉入的社交媒体按钮的点击次数。单击按钮时将打开一个新窗口。这是我的解决方案:

var iframeClick = function () {
    var isOverIframe = false,
    windowLostBlur = function () {
        if (isOverIframe === true) {
            // DO STUFF
            isOverIframe = false;
        }
    };
    jQuery(window).focus();
    jQuery('#iframe').mouseenter(function(){
        isOverIframe = true;
        console.log(isOverIframe);
    });
    jQuery('#iframe').mouseleave(function(){
        isOverIframe = false;
        console.log(isOverIframe);
    });
    jQuery(window).blur(function () {
        windowLostBlur();
    });
};
iframeClick();
于 2014-01-29T18:02:05.403 回答
2

您不能与不在同一域下的 iframe 进行交互。这总是被浏览器的策略所阻止。

请参阅使用 JavaScript 检测点击进入 iframe

于 2013-03-21T20:16:53.753 回答