如何触发iframe事件Jquery [英] How to trigger an iframe event Jquery

查看:339
本文介绍了如何触发iframe事件Jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个IFRAME,我想从IFRAME的父级触发一个事件(在iframe中):

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

只是我想做的一个简单例子:

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

推荐答案

您的jquery是正确的,唯一的问题是您在文档准备就绪时正在执行此操作,但那时iframe尚未完全加载,因此div不存在当时并不会触发点击. 将您的主页脚本更改为

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

这篇关于如何触发iframe事件Jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆