Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript window.addEventListener代码在移动设备上不起作用_Javascript_Jquery_Css_Responsive Design_Menu - Fatal编程技术网

Javascript window.addEventListener代码在移动设备上不起作用

Javascript window.addEventListener代码在移动设备上不起作用,javascript,jquery,css,responsive-design,menu,Javascript,Jquery,Css,Responsive Design,Menu,请我在我的JS代码中有问题,我构建了这个代码,因为我希望当访问者点击另一个div(不是菜单)时,如果它打开,菜单将关闭 这是代码,但此代码在手机上不起作用,但在chrome或firefox的开发工具中起作用 <script> window.addEventListener('mouseup', function(event){ var box = document.getElementById('narvbar_menu'); if (event.target !=

请我在我的JS代码中有问题,我构建了这个代码,因为我希望当访问者点击另一个div(不是菜单)时,如果它打开,菜单将关闭 这是代码,但此代码在手机上不起作用,但在chrome或firefox的开发工具中起作用

<script>
window.addEventListener('mouseup', function(event){
    var box = document.getElementById('narvbar_menu');
    if (event.target != box && event.target.parentNode != box){
        box.style.display="none";
        document.getElementById("close_menu").style.display="none";
    }
});
</script>

window.addEventListener('mouseup',函数(事件){
var box=document.getElementById('narvbar_菜单');
if(event.target!=box&&event.target.parentNode!=box){
box.style.display=“无”;
document.getElementById(“关闭菜单”).style.display=“无”;
}
});
致以最诚挚的问候

这是因为
“mouseup”
不会在移动设备上启动。只需收听
“touchend”

编辑:以下代码应将
“mouseup”
“touchend”
的事件侦听器附加到窗口

<script>
    ["mouseup", "touchend"].forEach(function(e) {
        window.addEventListener(e, function(event){
            var box = document.getElementById('narvbar_menu');
            if (event.target != box && event.target.parentNode != box){
                box.style.display="none";
                document.getElementById("close_menu").style.display="none";
            }
        });
    })
</script>

你试过使用“点击”事件而不是“鼠标点击”吗?这只是一个吹毛求疵的地方,在原生JS中,有参数的话,你只能附加一个事件类型。我已经编辑了我的解决方案,现在应该可以工作了,感谢@Teemu指出事件侦听器一次只能接受一个事件。
window.addEventListener("click", function(event) { ... }