将Click事件添加到iframe [英] Add click event to iframe

查看:589
本文介绍了将Click事件添加到iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将点击事件添加到 iframe 。我使用了这个例子并得到了这个:

I want to add a click event to an iframe. I used this example and got this:

$(document).ready(function () {
   $('#left').bind('click', function(event) { alert('test'); });
});

<iframe src="left.html" id="left">
</iframe>

但遗憾的是没有任何事情发生。

当我用另一个元素测试它时(例如一个按钮),它的工作原理:

But unfortunately nothing happens.
When I test it with another element (e.g. a button), it works:

<input type="button" id="left" value="test">


推荐答案

您可以将点击附加到iframe内容:

You could attach the click to the iframe content:

$('iframe').load(function(){
  $(this).contents().find("body").on('click', function(event) { alert('test'); });
});

注意:这仅适用于两个页面位于同一域中的情况。

Note: this will only work if both pages are in the same domain.

现场演示: http://jsfiddle.net/4HQc4/

这篇关于将Click事件添加到iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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