Triggering an event handler on an element inside iframe from parent frame

前端 未结 2 2095
北恋
北恋 2020-12-17 17:28

I have a html page. Inside it I use jQuery to attach click event on a link. Then I use iframe to reference this html page from another page. However I could not trigger the

相关标签:
2条回答
  • 2020-12-17 17:52

    Just place onload() event inside the iframe tag.

    <iframe src="http://myURL.com" onload="myFunction()">
    
    0 讨论(0)
  • 2020-12-17 17:55

    Here is how you can do it.

    document.getElementById('ifrmMsg').contentWindow.$('a:first').trigger('click');
    
    • Demo: http://jsbin.com/aYijiLe/2
    • Parent: http://jsbin.com/aYijiLe/2/edit
    • iFrame: http://jsbin.com/IrogeBi/1/edit

    You can also use postMessage() for this purpose. It will work cross-domain as well. However JS inside the iframe must listen to the message event, i.e. you need to control the source of the iframe.

    • Demo: http://jsbin.com/ILAsEmE/1
    • Parent: http://jsbin.com/ILAsEmE/1/edit
    • iframe: http://jsbin.com/uZEQuTa/2/edit
    0 讨论(0)
提交回复
热议问题