简体   繁体   English

在任意位置单击关闭菜单

[英]Closing a menu on anywhere click

I am working on a website and here is the link file:///D:/fahim/HTML/menu/index.html.我在一个网站上工作,这里是链接 file:///D:/fahim/HTML/menu/index.html。 If you click the menu its closing on its own "X" button, but i want it to close by clicking outside the menu.如果您单击菜单,它会在其自己的“X”按钮上关闭,但我希望通过在菜单外单击来关闭它。 This is the javascript used on the home page.这是主页上使用的javascript。

 <script> var popupView = new popup(); document.querySelector('#btn_1').addEventListener('click', function () { popupView.show(document.querySelector('#popup_1')); }); document.querySelector('#btn_2').addEventListener('click', function () { popupView.show(document.querySelector('#popup_2'), function () { console.log('show do something'); }); }); document.querySelector('#btn_3').addEventListener('click', function () { popupView.show(document.querySelector('#popup_3'), '', function () { console.log('CLOSE'); }); }); </script>

And this is the code which is attached as popup_view.js file on server这是在服务器上作为 popup_view.js 文件附加的代码

 (function () { var popup = function() { function hide(dom, dosomething) { if (!dom) { console.error('hide function not set dom object'); return; } if (dosomething) { dosomething(); } dom.className += ' ' + 'popup_hide'; } function show(dom, dosomethingShow, dosomethingClose) { if (!dom) { console.error('show function not set dom object'); return; } if (dosomethingShow) { dosomethingShow(); } var className = 'popup_hide', reg = new RegExp('(^|\\\\b)' + className.split(' ').join('|') + '(\\\\b|$)', 'gi'); dom.className = dom.className.replace(reg, '').trim(); var nodes = dom.childNodes; for (var i = nodes.length - 1; i >= 0; i--) { if (nodes[i].className === 'pop_up_close') { var close = function (e) { if (dosomethingClose) { dosomethingClose(); } dom.className += ' ' + 'popup_hide'; nodes[i].removeEventListener('click', close); }; nodes[i].addEventListener('click', close); break; } } } this.show = show; this.hide = hide; }; window.popup = popup; })();

Please help as i have tried lot of codes except these but non of them works请帮忙,因为我已经尝试了很多代码,但这些代码都不起作用

$(document).on('click', function(e){
  //your close function
  e.stopPropagation();
}

Put that code anywhere in your $(document).ready(... block将该代码放在$(document).ready(...块中的任何位置

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

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