简体   繁体   English

如何允许JS onclick使用指针事件:无;?

[英]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.

相关问题 pointer-events: 没有允许滚动但禁用 click-JS/CSS - pointer-events: none allow scroll but disable click-JS/CSS 如何禁用指针事件,但允许拖动 - How to disable pointer-events, but allow drag 如何在不使用指针事件的情况下选择绝对定位元素后面的文本:无 - How to allow selecting text behind an absolutely positioned element, without using pointer-events:none 如何将事件转移到画布上{指针事件:none}; (!在KonvaJS中) - How to transfer events into the canvas { pointer-events : none }; (!in KonvaJS) 如何在没有指针事件的情况下制作 cursor 指针 - How to make cursor pointer while pointer-events is none 赛普拉斯在使用条件时出现“pointer-events: none”错误,我该怎么办? - Cypress results with "pointer-events: none" error when using condition, what should I do? 如何使容器div“ pointer-events:none”但内容可点击…? - How to make container div “pointer-events:none” but content clickable…? 如何复制指针事件的行为:JavaScript中没有 - How to replicate the behavior of pointer-events:none in JavaScript 如何使Internet Explorer模拟指针事件:无? - How to make Internet Explorer emulate pointer-events:none? 指针事件没有在IE中表现异常 - pointer-events none not behaving as expected in IE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM