javascript - IFrame 按钮点击事件

标签 javascript iframe

我正在尝试将事件监听器附加到 IFrame 中页面上按钮的“单击”事件。 iframe 中的页面与父窗口属于同一域。

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

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

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

最佳答案

使用 jQuery:

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

或者不用 jQuery:

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

这仅在 iframe 满足 same origin policy. 时有效

关于javascript - IFrame 按钮点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13214522/

相关文章:

javascript - jQuery ajaxForm "h is undefined"问题

javascript - 无法访问 Google adx 返回的安全框架的内容

javascript - 使外部网页认为它在iframe中是完整尺寸

JavaScript/CSS : set (firefox) zoom level of iframe?

javascript - parent.document.getElementById ("...") 在 IE7 中为空或不是对象

javascript - Webpack,导入 SVG 的完整源代码而不是路径

javascript - 几次成功调用后的 Nodejs Node-SOAP ETIMEDOUT

javascript - 刷新问题?脚本在每次刷新时继续运行

javascript - 分析——head.js是如何实现并行资源序列化的?

javascript - 通过从下拉列表中选择来获取 iframe 中的特定网页内容