Attach click handler even on element inside iframe

2020-02-10 03:04发布

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.

标签: jquery iframe
2条回答
可以哭但决不认输i
2楼-- · 2020-02-10 04:03

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

查看更多
Animai°情兽
3楼-- · 2020-02-10 04:07

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

查看更多
登录 后发表回答