【问题标题】:Check mouse click on iframe检查鼠标点击 iframe
【发布时间】:2012-10-19 11:14:49
【问题描述】:

当鼠标点击 iframe 时如何检查 javascript,因为 iframe 不支持“on”事件?

那是我的 iframe:

我使用这个 jquery 代码:

$('#ifr31').bind('click', function(event) { alert(1) });

但是当我点击 iframe 时什么都不提醒?

【问题讨论】:

  • 你在使用同域的 iframe

标签: javascript jquery iframe


【解决方案1】:

取自这个答案:

https://stackoverflow.com/a/1609808/561545

iframe 没有“onclick”事件,但您可以尝试捕捉 点击 iframe 中文档的偶数:

document.getElementById("iframe_id").contentWindow.document.body.onclick = function() { alert("iframe 点击"); }

编辑虽然这不能解决您的跨站点问题,但仅供参考 jQuery 已更新以与 iFrame 配合使用:

$('#iframe_id').bind('click', function(event) { });

【讨论】:

    【解决方案2】:

    如果同源可以使用$('#ifame').bind('click', function(event) { });

    【讨论】:

      【解决方案3】:

      仅适用于同一域中的 iframe:

      $('body', $('select-your-iframe-here').contents()).click(function(event) {
        console.log('Clicked! ' + event.pageX + ' - ' + event.pageY);
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-18
        • 2021-09-06
        • 2015-08-03
        相关资源
        最近更新 更多