如何使用javascript捕获点击iframe

How to capture click on iframe using javascript

本文关键字:iframe 何使用 javascript      更新时间:2023-09-26

如何使用javascript捕获点击iframe

我使用这个代码:

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

但是它不能捕获iframe的任何点击。我只能使用javascript。我得到了这个错误"SecurityError: Blocked a frame with origin"

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");
}