简体   繁体   English

使用Invisible div检测Click Inside IFrame

[英]Detecting Click Inside IFrame Using Invisible div

I went through this post. 我仔细阅读了这篇文章。 Detect Click into Iframe using JavaScript 检测使用JavaScript点击进入Iframe

But still, I see people have done similar stuff. 但是,我仍然看到人们做过类似的事情。 Please tell me how do I do this. 请告诉我该怎么做。

One developer told me: 一位开发人员告诉我:

You could put a transparent DIV on top of the IFRAME. 您可以将透明DIV放在IFRAME之上。 You size that DIV to the same size or bigger than the IFRAME. 您将DIV的大小设置为与IFRAME相同或更大。 And with a higher z-index CSS property. 并具有更高的z-index CSS属性。

Then when you click on the IFRAME, the DIV receives the event. 然后当您单击IFRAME时,DIV会收到该事件。

But as the world is not perfect, now you lost the ability to click inside the IFRAME. 但是由于世界并不完美,现在你失去了在IFRAME中点击的能力。

But I am not so good with div's and looking to learn how to do this. 但我对div并不是很好,并希望学习如何做到这一点。 Thanks 谢谢

PS It is about Cross Domain or Alien Domain Iframing. PS它是关于跨域或外来域Iframing。

If I understand what you are asking here: 如果我明白你在这里问的是什么:

jsFiddle 的jsfiddle

// PLACE OVERLAY OVER EACH IFRAME
var W=0, H=0, X=0, Y=0;
$(".iframe").each(function(i,el){
   W = $(el).width();
   H = $(el).height();
   X = $(el).position().left;
   Y = $(el).position().top;
   $(this).after('<div class="overlay" />');
    $(this).next('.overlay').css({
        width: W,
        height: H,
        left: X,
        top: Y        
    });
});

// TRACK MOUSE POSITIONS (the overlay will prevent clicks on iframe page)
var mx = 0, my = 0;
$('.overlay').on('mousemove click',function(e){
    mx = e.clientX - $(this).position().left;
    my = e.clientY - $(this).position().top;

    if(e.type==='click'){
        alert('clicked at: X='+mx+' Y='+my)
    }        
});

I think a transparent div will not work, because it will catch all click events, and not spread it down to the iframe. 我认为透明div不起作用,因为它将捕获所有点击事件,而不是将其传播到iframe。

So the only workaround I know to track click on iframes is using the blur event. 因此,我知道跟踪点击iframe的唯一解决方法是使用模糊事件。 You can use the iframeTracker jQuery Plugin to do this easily : 您可以使用iframeTracker jQuery插件轻松完成此操作:

jQuery(document).ready(function($){
    $('.iframe_wrap iframe').iframeTracker({
        blurCallback: function(){
            // Do something when iframe is clicked (like firing an XHR request)
        }
    });
});

Check out Jquery's .live method. 查看Jquery的.live方法。

It can attach an event handler to a selector now and in the future, such as when content is loaded into a div. 它现在和将来可以将事件处理程序附加到选择器,例如将内容加载到div中时。

http://api.jquery.com/live/ http://api.jquery.com/live/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM