我基本上试图从event.target获取目标元素,如果不是相同的进程.我怎样才能得到以下的东西?
$(document).ready(function(){
$(document).click(function(event) {
if(event.target!='#contents') { //If clicked element is not div #contents show alert .. //How can i achieve this ?
alert("..");
}
});
});
Run Code Online (Sandbox Code Playgroud)
Nic*_*tti 15
使用
//If clicked element id is not contents
if(event.target.id !== 'contents') {
alert("..");
}
Run Code Online (Sandbox Code Playgroud)
编辑 - 如果结构是您的评论使用结构:
if($(event.target).closest('div#contents').length === 0){
alert('there is no div with id === "contents" in the ancestors of the clicked li');
}
Run Code Online (Sandbox Code Playgroud)
编辑2 - 我的代码的解释.如果你有这个标记
<div id="contents"><ul><li>Item1</li><li>Item2</li></ul></div>
Run Code Online (Sandbox Code Playgroud)
这段代码意味着
//event.target is a DOM element, so wrap it into a jQuery object
$(event.target)
.closest('div#contents')//find a div element going up the dom tree.
//This method returns a jQuery collection that it's
//empty if the div is not found
.length //the collection has a length
//property that is equal to the number of found elements
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30654 次 |
| 最近记录: |