Attach click handler even on element inside iframe with jQuery

可紊 提交于 2019-12-18 03:59:14

问题


How can I attach a click event handler to an element inside an iframe?

Here's what I tried and doesn't work:

$("#parent iframe").contents().find("a").live("click", function () {
    alert ("test");
    return false;
});

The iframe is on the same domain.


回答1:


Discovered the problem. LIVE does not work inside an iframe. Switching to bind works fine. Will do manual event delegation instead.




回答2:


Basically probably not as at a guess the content is likely to be from a different domain (usual use of iFrames). These articles explain why:

iframes and cross domain security Cross-Domain Ajax Insecurity

Alternatively you will need to modify the source code in the iFrame (if you have control over it) or you can ask users to reduce their browser security (might be possible on an intranet, but even then not a good idea).



来源:https://stackoverflow.com/questions/1655862/attach-click-handler-even-on-element-inside-iframe-with-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!