我有一个这样的函数:
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.
});
如何使用 if 语句检查 e.target
是否与 $('ul#menu-navigation > li.menu-item') 匹配
或此特定 div 内的任何子元素。
例如,类似:
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
}
最佳答案
使用closest()
从目标开始并向上查找
jQuery(document).on('click touch', function(e) {
if($(e.target).closest('ul#menu-navigation > li.menu-item').length){
// is within a menu item
}
});
关于javascript - $(document.documentElement) 目标特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42346598/