简体   繁体   中英

Trigger "click" event in iframe

Is it possible to trigger a click event in cross domain iframe with javascript or jquery?

<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>

Example : Trigger click event on div(#buttonB) when user click "buttonA"

You cannot detect click event in cross-domain iframe but one way is that you can detect focus on that iframe.

First click outside of iframe and then on iframe! You will see the alert.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM