簡體   English   中英

在 iframe 中觸發“點擊”事件

[英]Trigger "click" event in iframe

是否可以使用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>

$('#buttonB').on('click',function(){
    ('#buttonA').click();
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM