jQuery:点击iframe内的事件 [英] jQuery: click event inside iframe

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

问题描述

我试图在点击iframe中的某个元素时触发一个点击事件,但它似乎不起作用。我目前的设置:

jsFiddle: http://jsfiddle.net/q4aa3/

jQuery:

  $(document).ready(function(){
$( '(this#iframe')。load(function(){
$('#this-iframe')。contents()。find('img')。live({
click:function( ){
alert('clicked img');
}
});
});
});

点击iframe内的图片不会触发警报,我不知道为什么,还是有更好的方法来实现这一目标?任何建议都将不胜感激!

解决方案

当您在同一个域中使用iframe时,可以使用此脚本来捕获点击在iframe中。

  var iframeBody = $('body',$('body',$( '#iframe')[0] .contentWindow.document); 
$(iframeBody).on('click','img',function(event){
doSomething();
});

您可以通过iframeBody变量来操纵主体。

I'm trying to fire a click event on the click of an element inside an iframe, but it doesn't seem to be working. My current set up:
jsFiddle: http://jsfiddle.net/q4aa3/
jQuery:

$(document).ready(function () {
    $('#this-iframe').load(function () {
        $('#this-iframe').contents().find('img').live({
            click: function () {
                alert('clicked img');
            }
        });
    });
});

Clicking on the image inside the iframe isn't firing the alert, I'm not sure why, or is there a better way to achieve this? Any suggestions would be greatly appreciated!

解决方案

When you have the iframe on the same domain, you can use this script to catch clicks in the iframe. Don't use .live, it is depriciated as of jQuery 1.7.

var iframeBody = $('body', $('#iframe')[0].contentWindow.document);
$(iframeBody).on('click', 'img', function(event) {
    doSomething();
});

You can manipulate the body through the iframeBody variable.

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

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