简体   繁体   English

使用jQuery使覆盖div不可点击

[英]Make a overlay div not clickable using jquery

I have a problem, I have a semi-transparent div over my menu on a page, and I want to make that div click-through. 我有问题,页面上的菜单上方有一个半透明的div,我想使该div点击。

The CSS property pointer-events is not an option because I need to generate valid CSS. CSS属性指针事件不是一个选项,因为我需要生成有效的CSS。 I tried with: 我尝试过:

event.preventDefault();
event.stopPropagation();

with no luck, the div on the "bottom layer" doesn't be clicked. 运气不好,不会单击“底层”上的div。

Here is my code in a simplified version: 这是我的简化版本代码:

<div id="menuContainer">
<div id="menu">
//here comes all the items of my menu
</div>
</div>
<div id="shadow">
</div>

the css: CSS:

#menuContainer {
position: absolute;
top: 0px;
z-index: 0;
height: 200px;
}
#menu {
width: 300px;
height: 30px;
margin: 0 auto;
position: absolute;
z-index: 10; 
top: 160px;
}
#shadow {
position: absolute;
z-index: 1;
top: 150px
height: 200px;
}

is not an option for me to change the z-index or the layout, just I need when I click the shadow, the click is "re-transmitted" to the menu. 我不能选择更改z-index或布局,仅当我单击阴影时才需要,单击将“重新传输”到菜单中。

Have you looked into using jQuery's click() function? 您是否考虑过使用jQuery的click()函数?

http://api.jquery.com/click/ http://api.jquery.com/click/

You could catch the click event on the topmost element, simulate a click on the element below, and then return to stop the original click. 您可以在最上面的元素上捕获click事件,在下面的元素上模拟单击,然后返回以停止原始单击。

You could do something like this 你可以做这样的事情

$('#shadow').click(function() {
    $('#menu').trigger('click');
});

The shadow element is passing the click event on to the menu. 阴影元素将click事件传递到菜单上。

If you need to relay the mouse position as well, check out the Mouse Position tutorial. 如果您还需要中继鼠标位置,请查看“ 鼠标位置”教程。

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

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