从父框架触发 iframe 内元素的事件处理程序 [英] Triggering an event handler on an element inside iframe from parent frame

查看:21
本文介绍了从父框架触发 iframe 内元素的事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 html 页面.在其中,我使用 jQuery 在链接上附加点击事件.然后我使用 iframe 从另一个页面引用这个 html 页面.但是我无法使用 js 触发附加在该链接上的事件(当我用鼠标单击链接时,事件被触发没有问题).如何从父框架触发该事件?我尝试了以下方法,但它不起作用:

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 event attached on that link with js (when I click the link with the mouse, the event gets triggered with no problem). How can I trigger that event from parent frame? I have tried the following but it does not work:

var frame = $('#mainFrame'); // the iframe I am trying to access
var link = $('a.btn', frame.contents()); // the element is found correctly
link.trigger('click');  // nothing happens. 
var e = link.data('events');  // e is undefined. 

推荐答案

你可以这样做.

document.getElementById('ifrmMsg').contentWindow.$('a:first').trigger('click');

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