简体   繁体   English

如何触发iframe事件Jquery

[英]How to trigger an iframe event Jquery

I have an IFRAME and I want to trigger an event (which is inside the iframe) from the parent of an IFRAME: 我有一个IFRAME,我想从IFRAME的父级触发一个事件(在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>

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. 您的jquery是正确的唯一问题是,当文档准备就绪时您正在执行此操作但当时iframe未完全加载,因此当时div不存在而不触发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");
    });
});

try this 尝试这个

ger javascript event from within an iFrame 来自iFrame的ger javascript事件

but look at answer from Gilly 但看看吉利的回答

document.myFrameName.myFunction(); 

regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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