【问题标题】:How to capture click on iframe using javascript [duplicate]如何使用javascript捕获点击iframe [重复]
【发布时间】:2014-06-10 13:59:04
【问题描述】:

如何使用 javascript 捕获 iframe 上的点击。

我正在使用此代码:

document.getElementsByClassName('pin').contentWindow.document.body.onclick = 
function() {
  alert("iframe clicked");
}

但它无法捕获 iframe 的任何点击。我只能使用javascript。 我收到此错误 "SecurityError: Blocked a frame with origin"

【问题讨论】:

  • iframe 是否来自同一个主机/域?
  • 看看这是否有帮助:stackoverflow.com/questions/1609741/…
  • getElementsByClassName 返回一个没有contentWindow 属性的NodeList。要么尝试document.getElementsByClassName('pin')[0],要么在名单上走。

标签: javascript html iframe


【解决方案1】:
var i = 0; 
//i represents the exact node since obtaining node by class name gives array of nodes.
var iframe = document.getElementsByClassName('pin')[i].contentWindow;

var iframe = document.getElementById("iframe_id").contentWindow;

iframe.document.body.onmousedown = 
function() {
  alert("iframe clicked");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 2011-04-11
    • 1970-01-01
    • 2011-01-08
    • 2012-07-13
    • 1970-01-01
    相关资源
    最近更新 更多