[jQuery]在iFrame中查找单击 [英] [jQuery]Find click inside an iFrame

查看:102
本文介绍了[jQuery]在iFrame中查找单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含iframe的页面。

I have a page with an iframe.

<div id="my_content">
  <iframe id="my_iframe" src="http://mysite.com/iframe.html">
</div>

我需要拦截iframe内的点击。我已尝试使用此代码,但它不起作用:

I need to intercept the click inside the iframe. I've tried with this code but it doesn't work:

<script type="text/javascript">


jQuery(document).ready(function(){
 jQuery('#my_iframe').click(function(){ 
  alert('click inside iframe');
 });
});


推荐答案

假设iframe源位于同一个域上在外页,您可以使用内容方法

Assuming that the iframe source is on the same domain as the outer page, you can use the contents method:


如果iframe与主页位于同一个域,则.contents()方法也可用于获取iframe的内容文档。

The .contents() method can also be used to get the content document of an iframe, if the iframe is on the same domain as the main page.



jQuery(document).ready(function(){
  jQuery('#my_iframe').contents().click(function(){ 
    alert('click inside iframe');
  });
});

这篇关于[jQuery]在iFrame中查找单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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