【问题标题】:Jquery capture Click on Iframejquery捕获点击iframe
【发布时间】:2013-03-21 20:14:31
【问题描述】:

所以我的页面上有一个 iframe,比如

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

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

【问题讨论】:

标签: jquery iframe


【解决方案1】:

我遇到了一种情况,我必须跟踪通过 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();

【讨论】:

  • 这正是我需要帮助的场景。您的解决方案很巧妙,谢谢!
  • 请注意,这只适用于第一次点击。 (除非鼠标离开并重新进入 iframe)
【解决方案2】:

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

Detect Click into Iframe using JavaScript

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 2011-01-08
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多