【问题标题】:Trigger "click" event in iframe在 iframe 中触发“点击”事件
【发布时间】:2020-01-07 13:38:33
【问题描述】:

是否可以使用 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) 上的单击事件

【问题讨论】:

  • 简答:否
  • @Pedram 好的,谢谢您的回答

标签: javascript jquery


【解决方案1】:

您无法检测跨域 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>

【讨论】:

  • iframe 已隐藏,因此用户将无法查看和点击 iframe。当用户单击我页面上的按钮('buttonA')时,我想在 iframe 内的按钮('buttonB')上触发点击事件。谢谢分享顺便说一句
【解决方案2】:
$('#buttonB').on('click',function(){
    ('#buttonA').click();
});

【讨论】:

    猜你喜欢
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2023-04-04
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    • 2013-03-02
    相关资源
    最近更新 更多