IFrame按钮单击事件 [英] IFrame button click event

查看:112
本文介绍了IFrame按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将eventlistener附加到IFrame中页面上按钮的click事件。 iframe中的页面与父窗口属于同一个域。

I'm trying to attach an eventlistener to the "click" event of a button on a page in an IFrame. The page in the iframe belongs to the same domain as the parent window.

window.frames["iframe_Id"].document.getElementById("button_id").addEventListener("click",functionToRun,false);

上述示例对我不起作用。它可能只是我的实际代码中的语法错误,但我想看看这是否是一般的想法。

The above sample is not working for me. It could just be a a syntax error in my actual code but I wanted to see if this is the general idea.

注意:我发现了很多关于将click事件添加到整个Iframe的示例,但我不确定它对Iframe中的按钮是否有效。

Note: I found plenty of examples on adding the click event to the entire Iframe but I'm not sure it works the same for buttons within the Iframe.

推荐答案

使用jQuery:

$("#iframe_Id").contents("#button_id").click(functionToRun);

或者没有jQuery:

Or without jQuery:

document.getElementById("iframe_Id").contentWindow.document.getElementById("button_id").addEventListener("click", functionToRun, false);

这仅适用于iframe符合同源政策。

This will only work if the iframe meets the same origin policy.

这篇关于IFrame按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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