[英]How do I allow JS onclick with pointer-events:none;?
I have a div
over the whole page to close a dropdown menu when the big div
is clicked. 单击大
div
时,我在整个页面上都有一个div
,以关闭下拉菜单。 The thing is that I need pointer-events: none;
问题是我需要
pointer-events: none;
because if I don't use it, the whole page gets blocked by the big div
. 因为如果我不使用它,整个页面就会被big
div
阻塞。
JS onclick
won't work when I have pointer-events:none;
当我有
pointer-events:none;
时,JS onclick
无法正常工作pointer-events:none;
So, I don't really know how to solve this. 所以,我真的不知道如何解决这个问题。
function test() {
if (document.getElementById('div1').style.display == 'block') {
document.getElementById('div1').style.display = 'none';
}
else{
}
}
#big_div{
width: 100%;
height: 100%;
display: block;
pointer-events:none;
}
<div id="big_div" onclick="test()"></div>
Instead of using a div covering your whole page, put a click listener on the document, check to see if the clicked element is the menu or a child of the menu, if not then hide the menu 不用在整个页面上使用div,而是在文档上放置单击侦听器,检查单击的元素是菜单还是菜单的子菜单,如果不是,则隐藏菜单
document.addEventListener("click",function(e){
var menu = document.getElementById("myMenu");
var target = e.target;
if(target !== menu && !menu.contains(target)){
menu.style.display = "none";
}
});
Demo 演示版
document.addEventListener("click",function(e){ var menu = document.getElementById("myMenu"); var target = e.target; var openBtn = document.querySelector("button"); if(target !== menu && !menu.contains(target) && target !== openBtn){ menu.style.display = "none"; } }); document.querySelector("button").addEventListener("click",function(){ document.getElementById("myMenu").style.display = "block"; });
menu { width:120px; height:300px; background:#88DDFF; display:none; }
<menu id="myMenu"><span>some item</span></menu> <button>Open menu</button>
pointer-events: none
means no events will come through. pointer-events: none
表示没有事件通过。 Instead, you should close the menu by listening to click/mousedown events on the entire document (and remove your div that is set to pointer-events: none
). 相反,您应该通过侦听整个文档上的click / mousedown事件来关闭菜单(并删除设置为
pointer-events: none
div)。
document.addEventListener('mousedown', function(e) {
// You may need a better check involving e.target because
// you won't want to close the menu when clicking inside the menu
// or on the button (if the menu is not open)
if (!e.target.contains(menuNode)) {
document.getElementById('div1').style.display = 'none';
}
});
Sorry, I didn't read your question carefully so I got downvotes for my wrong answer. 抱歉,我没有仔细阅读您的问题,所以我为我的错误回答投了反对票。
But, according to your question, you want to cover the whole page with that div
to block the click event but you still want to receive the click event then you can do like this actually: 但是,根据您的问题,您想用该
div
覆盖整个页面以阻止click事件,但是您仍然希望接收click事件,那么您实际上可以这样做:
1) Remove pointer-events:none;
1)删除
pointer-events:none;
from that div
and add the cursor
: 从该
div
添加cursor
:
#big_div {
width: 100%;
height: 100%;
display: block;
cursor: none;
}
2) Add the listener to your div
like I previously mentioned and prevent the click from there: 2)像我之前提到的那样,将侦听器添加到您的
div
中,并防止从那里单击:
document.getElementById("big_div").addEventListener("click", function(e) {
e.preventDefault();
// Do whatever you want to do
if (document.getElementById('div1').style.display == 'block') {
document.getElementById('div1').style.display = 'none';
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.