简体   繁体   English

$(document.documentElement)目标特定元素

[英]$(document.documentElement) target specific element

I have a function like this: 我有一个这样的功能:

jQuery(document.documentElement).on('click touch', function(e) {
    // check if $(e.target) matches with $('ul#menu-navigation > li.menu-item')
    // or any child elements within this particular div.
});

How do I make an if statement checking if e.target matches with $('ul#menu-navigation > li.menu-item') or any child elements within this particular div. 如何制作if语句,检查e.target是否与$('ul#menu-navigation > li.menu-item')特定div中的任何子元素匹配。

For example, something like: 例如,类似:

if ( $(e.target) == $('ul#menu-navigation > li.menu-item') ) {
    // user clicked on this div or any child divs within this element
} else {
    // user clicked anywhere outside of this div
}

Use closest() to start at target and look up 使用closest()从目标开始并查找

jQuery(document).on('click touch', function(e) {
    if($(e.target).closest('ul#menu-navigation > li.menu-item').length){
      // is within a menu item
    }   
});

Use jQuery.is like this: 使用jQuery.is是这样的:

if ( $(e.target).is('ul#menu-navigation *') ) {
    // user clicked on this div or any child divs within this element
} else {
    // user clicked anywhere outside of this div
}

And if you want to include only ul#menu-navigation > li.menu-item and children of li.menu-item use this selector: 'ul#menu-navigation > li.menu-item, ul#menu-navigation > li.menu-item *' 如果你想只包括ul#menu-navigation > li.menu-item和儿童li.menu-item使用这个选择: 'ul#menu-navigation > li.menu-item, ul#menu-navigation > li.menu-item *'

Try use if ($(e.target).is('ul#menu-navigation > li.menu-item')) 尝试使用if ($(e.target).is('ul#menu-navigation > li.menu-item'))

This will ask if the element match. 这将询问元素是否匹配。

 jQuery(document.documentElement).on('click touch', function(e) { if ($(e.target).is('ul#menu-navigation > li.menu-item')) { // user clicked on this div or any child divs within this element alert("I clicked on LI") } else { // user clicked anywhere outside of this div } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul id="menu-navigation"> <li class="menu-item">Home</li> <li class="menu-item">About</li> </ul> 

I can not really understood the benefit of this function. 我无法真正理解此功能的好处。 I can imagine you can resolve the problem by using e.currentTarget instead of e.target , when you dont bind the event to document.documentElement . 我可以想象,当您不将事件绑定到document.documentElement时,可以通过使用e.currentTarget而不是e.target来解决问题。 Bind it to your target, in this case I think it should be $('ul#menu-navigation > li.menu-item') 将其绑定到目标,在这种情况下,我认为应该是$('ul#menu-navigation > li.menu-item')

$('ul#menu-navigation > li.menu-item').on('click', function (event) {
    var $target = $(event.currentTarget);
})

With this solution, you know it is the element what you want and you reduce the overhead, because you dont listen to every klick somewhere in your document. 使用此解决方案,您就知道它是您想要的元素,并且减少了开销,因为您无需听文档中的每个杂音。

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

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