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

查看:46
本文介绍了将点击事件添加到 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/

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

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