[英]how to check the parent div, from click event on the page from jquery
I want to check if I am clicking inside the div with classname as 'myclass' (in any child inside the div ). 我想检查是否在div内单击类名为'myclass'的div(在div内的任何子级中)。 this code is returns only the current control in which we click , I want to check its all parents , if it has div with classname 'myclass' 此代码仅返回我们单击的当前控件,如果它的类名为“ myclass”,我想检查其所有父级
$("body").click(function (e) {
if (e.target.className !== "myclass") {
doo();
}
alert(e.target.id);
alert(e.target.className);
});
$("body").click(function (e) {
var elem = $('.myClass');
if ((!elem.is(e.target)) && (elem.has(e.target).length === 0)) {
doo();
}
});
you can use closest to find the parent which is having myclass
您可以使用最接近的找到具有myclass
的父级
try this 尝试这个
$("body").click(function (e) {
if($(e.target).closest('.myclass').length == 0 && $(e.target).attr('class') != "myclass"){
doo();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.