是否可以用javascript或jquery触发跨域iframe中的点击事件?
<html>
<head>...</head>
<body>
<div id='divA'>
<button type='button' id='buttonA'>Button</button>
</div>
<div id='divB'>
<iframe name='random'>
<div role='button' id='buttonB'></div>
</iframe>
</div>
</body>
</html>
示例: 当用户点击“buttonA”时触发div(#buttonB)上的点击事件
请您参考如下方法:
您无法检测跨域 iframe 中的点击事件,但一种方法是您可以检测该 iframe 上的焦点。
首先点击 iframe 外部,然后点击 iframe!您将看到警报。
window.focus(); //force focus on the currenct window;
window.addEventListener('blur', function(e){
if(document.activeElement == document.querySelector('iframe'))
{
alert('Focus Left Current Window and Moved to Iframe / Possible click!');
}
});
<iframe name='random'>
<div role='button' id='buttonB'></div>
</iframe>
