How to trigger an iframe event Jquery

旧城冷巷雨未停 提交于 2020-01-04 05:35:21

问题


I have an IFRAME and I want to trigger an event (which is inside the iframe) from the parent of an IFRAME:

Just a rough example of what I am trying to do :

<iframe id="i">
  <div id="test"></div>
  <script>
  $(document).ready(function() {  // Event is binded inside the iframe 

   $("#test").live({ click : function() { alert("hello"); } });
  });
  </script>
</iframe>

<script>
$(document).ready(function() { // Want to trigger it from outside the iframe
 $("#i").contents().find("#test").trigger("click");
});
</script>

回答1:


Your jquery is correct only problem is that that you are doing this when document is ready but at that time iframe is not getting fully loaded therefore div doesn't exist at that time and not triggers click. Change your main page script to

$(document).ready(function() {
    //want to trigger it from outside the iframe
    $("#i").load(function(){
        $("#i").contents().find("div").trigger("click");
    });
});



回答2:


try this

ger javascript event from within an iFrame

but look at answer from Gilly

document.myFrameName.myFunction(); 

regards



来源:https://stackoverflow.com/questions/10943727/how-to-trigger-an-iframe-event-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!