Jquery - 拦截链接在iframe中单击 [英] Jquery - intercept links clicked inside an iframe

查看:252
本文介绍了Jquery - 拦截链接在iframe中单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图拦截点击页面上的链接,包括iframe内的链接。这是我的代码,但它不起作用。任何想法我需要做什么?

I am trying to intercept links clicked on a page including those inside an iframe. This is the code that I have but it is not working. Any ideas what I need to do?

$("#container").delegate('a', 'click', function(e){ 
//do stuff
}

Container is the iframe里面的div的ID。

Container is the id of the div just inside the iframe.

提前感谢任何建议

推荐答案

您需要进入< iframe> 并在那里设置代理,您可以这样做:

You need to reach inside the <iframe> and set the delegate there, you can do it like this:

$('#myiframe').contents().find("#container").delegate('a', 'click', function(e){ 
  //do stuff
}

编辑 - Mailslut 在下面提出了一个很好的观点,如果iframe不在同一个域(和端口)上,你就不能做这样的事了。就是这种情况,你想了解更多关于原因的信息,阅读同源政策 e出于安全原因。

Edit - Mailslut makes a good points below, if the iframe isn't on the same domain (and port), you can't do anything like this. If that's the case and you want to know more about why, read about the same-origin policy there for security reasons.

这篇关于Jquery - 拦截链接在iframe中单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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