jQuery 如果元素没有具有特定类的父元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15680580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If element does not have parent with specific class
提问by Tom
I am trying to create an if statement in jQuery for elements that have a parent with a specific class.
我正在尝试在 jQuery 中为具有特定类的父元素的元素创建一个 if 语句。
This is what I've come up with so far but it's not right.
到目前为止,这是我想出的,但这是不对的。
if(!$(".myElem").parents(".myDiv")) {
console.log("test")
};
Can anyone point me in the right direction please.
任何人都可以指出我正确的方向。
回答by adeneo
Use length
to check it there are elements in the selector, and closest()
would be better than parents()
as it stops once it finds a match:
使用length
检查它有在选择的元素,并且closest()
会比parents()
它停止一旦找到一个匹配:
if(! $(".myElem").closest(".myDiv").length ) {
console.log("has no parent with the class .myDiv")
}
回答by Arun P Johny
If you are testing whether the element with class myElem
has a direct parent with class myDiv use the following test
如果您正在测试具有 class 的元素是否myElem
具有具有 class myDiv 的直接父级,请使用以下测试
if(!$(".myElem").parent().hasClass("myDiv")) {
console.log("test")
};
回答by IT ppl
if($(".myElem").parent().hasClass(".myDiv")) {
console.log("has a parent with the class .myDiv")
}
else
{
console.log("no parent class .myDiv found")
}
回答by Zoltan.Tamasi
This should select those elements that have got class myElem with a parent that has a class myDiv:
这应该选择那些具有类 myElem 且父类具有类 myDiv 的元素:
$(".myElem").filter(function(elem) {
return $(this).parents(".myDiv").length
});
or this should select those elements that have got class myElem and does not have a parent that has a class myDiv:
或者这应该选择那些具有类 myElem 并且没有具有类 myDiv 的父元素的元素:
$(".myElem").filter(function(elem) {
return !$(this).parents(".myDiv").length
});
回答by Marc Nicole
the only solution that works if $('.myElem') has more than one element is the one from Zoltan
如果 $('.myElem') 具有多个元素,则唯一有效的解决方案是 Zoltan 中的一个
see the example here
看这里的例子
var takeElements = $('.myElement').filter(function(){
return $(this).closest('.bannedAncestors').length === 0
});
takeElements.css('color','green');
var bannedElements = $('.myElement').filter(function(){
return $(this).closest('.bannedAncestors').length !== 0
});
bannedElements.css('color','red');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="bannedAncestors">
<div> number of nested element don't matters
<div class="myElement">
don't take this child since it has an ancestor we don't want
</div>
</div>
</div>
<div>
<div>
<div class="myElement">
but this one is ok
</div>
</div>
</div>
回答by karthick
Try this one.
试试这个。
if($("#child").parent("span#parent").length > 0)
{
console.log("parent is span");
}
else {
console.log("parent is not span or no parent");
}
回答by Sanjay
A better approach is to use .closest()
if you know the classname of the child.
.closest()
如果您知道孩子的类名,则更好的方法是使用。
if(!$('yourtag.childClass').closest('yourParentTag').hasClass('classname')){
console.log('does not exist');
}