简体   繁体   English

window.onclick除非元素是'someElement'

[英]window.onclick except if element is 'someElement'

I am wondering if there is a way to exclude certain elements on a function. 我想知道是否有办法排除函数上的某些元素。 For example: 例如:

window.onclick = doThis() except if the element clicked is a 'ul' element.

Something like that. 这样的事情。 I know that it would probably have to be written like this: 我知道它可能必须这样写:

function doThis() {
  if (this item clicked is NOT 'ul') { 
     function xyz();
}}

window.onclick = doThis();

I am just not sure if there is an "exception" in javascript. 我只是不确定javascript中是否存在“异常”。 Can someone please help. 有人可以请帮助。 thanks. 谢谢。

ok so here is my full code because it is still not working: 好的,所以这里是我的完整代码,因为它仍然无法正常工作:

<script type="text/javascript">
function ddMenu() {
var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function ddMenu_open(e)
{
   //ddMenu_canceltimer();
   ddMenu_close();
   var submenu = $(this).find('ul');
    if(submenu){
        ddmenuitem = submenu.css('visibility', 'visible');
        //return false;
        //preventDefault();

    }
   //return true;
}

function ddMenu_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden'); }

function ddMenu_timer()
{ closetimer = window.setTimeout(ddMenu_close, timeout); }

function ddMenu_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}

$(document).ready(function()
{  $('#ddMenu > li').bind('click', ddMenu_open);
   $('#ddMenu li ul').bind('click', ddMenu_timer);
   //$('#ddMenu > li').bind('mouseout',  ddMenu_timer);
   //$('#ddMenu > li').bind('mouseover', ddMenu_canceltimer);

});

document.onclick = function(ev){
    if(ev.target.nodeName !== 'ul')  {
        ddMenu_close();
    }
};


}
</script>

i added the target.nodeName !== code that was suggested below but it is still not working. 我添加了下面建议的target.nodeName!==代码,但它仍然无效。 When i click a ul it still closes my menu. 当我点击一个ul它仍然关闭我的菜单。 thank you. 谢谢。

window.onclick = function(ev){
    if( ev.target.nodeName !== 'UL' ){
        xyz();
    }
};

What we're doing here is making the event object a parameter. 我们在这里做的是将事件对象作为参数。 This way we can get the target of the click event. 这样我们就可以获得click事件的目标。 We're doing exactly that here—we check if the target is a ul element. 我们正在这里做 - 我们检查目标是否是ul元素。 If not, run the function. 如果没有,请运行该功能。

Also, just for future reference, you have to assign the function to the onclick. 此外,仅供将来参考,您必须将该功能分配给onclick。 So first assign the function to a variable, and then assign the variable to the onclick (without parentheses). 因此,首先将函数分配给变量,然后将变量分配给onclick(不带括号)。 I read this somewhere...I'll try to pull that up. 我在某个地方读到这个......我会试着把它拉出来。

Events bubble up the DOM tree, so just set a "blocker" that prevents the bubbling from all <ul> s: http://jsfiddle.net/6sqWh/ . 事件冒出DOM树,所以只需设置一个阻止所有<ul> s冒泡的“阻止程序”: http//jsfiddle.net/6sqWh/

$(window).on("click", doThis);

$("ul").click(false); // prevent bubbling to window from <ul>s

Visually it will go like this: 在视觉上它会像这样:

execute the function
 ^                    (passed through)
window  <- click

--   blocks
ul

in contrast with: 与...相比:

execute the function
                      (not executed)
window
                      (won't arrive here)
--  blocks
ul  <- click

Note that without the blocker, it would go like: 请注意,没有阻止程序,它会像:

execute the function
 ^
window
 ^
ul  <- click

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

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