简体   繁体   English

jQuery的。单击元素与元素背后的堆叠错误?

[英]jQuery .click on element with element behind stacking error?

I have a 'lightbox' similar function which simply brings up 2 divs like this: 我有一个类似“灯箱”的功能,它简单地显示了2个div,如下所示:

http://i.imgur.com/3ZhEz.jpg

The green color (wont be colored in once it works) is the area to click to close the login box. 绿色(一旦起作用,将不会变成彩色)是单击以关闭登录框的区域。

jsFiddle with code: http://jsfiddle.net/bhcGv/8/ 带有代码的jsFiddle: http//jsfiddle.net/bhcGv/8/

When you click on the login box it will disappear as it thinks its clicking on the green box. 当您单击登录框时,它将消失,因为它认为单击了绿色框。 Any suggestions? 有什么建议么?

you need .stopPropagation method. 您需要.stopPropagation方法。

here the updated jsfidle: http://jsfiddle.net/bhcGv/10/ 这里是更新的jsfidle: http : //jsfiddle.net/bhcGv/10/

you could add this code 您可以添加此代码

.find('#centeredBox').click(function(e){
        e.preventDefault();
        e.stopPropagation();
return false;
    });

so your document.ready function becomes 因此您的document.ready函数变为

$(document).ready(function(){
    $("#loginOffBox").click(function(){
        $('#loginOffBox').hide();
          $('#centeredBox').hide();
    }).find('#centeredBox').click(function(e){
        e.preventDefault();
        e.stopPropagation();
return false;
    });
    // other js
});

this should stop the event from being run when you click on the centered div. 当您单击居中的div时,这应阻止事件运行。

You could also take the centered div out of the colored one too. 您也可以从有色div中删除居中的div。 then you wont have the event bubbles. 那么您就不会有事件泡沫。

You want to fix the z-index of the two divs. 您要修复两个div的z-index。 Background div should have a smaller z-index than the popup div and both should have a relatively high value. 背景div的z-index值应小于弹出div的值,并且两者均应具有较高的值。

Try z-index of 1000 for loginOffBox and 1100 for centeredBox and it should work fine. 对于loginOffBox尝试使用1000的z-index,对于centeredBox尝试使用1100的z-index,它应该可以正常工作。

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

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