[英]jQuery selector only if parent doesn't have attr
Imagine I have this: 想象一下我有这个:
<span email="a@test.com" class="parent">
<div class="child"></div>
</span>
<span email="b@test.com" class="parent">
<div class="child"></div>
</span>
How I would I find only the .child
elements where the parent span attr email
is NOT b@test.com
? 我如何仅查找父跨度attr email
不是b@test.com
的.child
元素?
Use this selector: 使用此选择器:
$(".parent:not([email='b@test.com']) .child")
You can use .each to get each element: 您可以使用.each来获取每个元素:
$(".parent:not([email='b@test.com']) .child").each(function(){
var item = $(this); // get the current element
// do something
});
It's simple as this: 就这么简单:
$('[email][email!="b@test.com"] > .child')
Explanation: 说明:
With this you find all elements with class child
which have a parent ( >
) with email attribute ( [email]
) and this email attribute is not b@test.com
( [email!="b@test.com"]
). 这样,您可以找到所有child
类的元素,这些元素的父( >
)具有电子邮件属性( [email]
),并且此电子邮件属性不是b@test.com
( [email!="b@test.com"]
)。
Just use one of those selectors: 只需使用以下选择器之一:
.parent:not([email="b@test.com"]) > .child
.parent:not([email=b\@test\.com]) > .child
.parent:not([email="b@test.com"]) > .child { color: red; }
<span email="a@test.com" class="parent"> <div class="child">A</div> </span> <span email="b@test.com" class="parent"> <div class="child">B</div> </span>
If you want to get them all with JS, use querySelectorAll
: 如果要使用JS来全部获取它们,请使用querySelectorAll
:
document.querySelectorAll('.parent:not([email="b@test.com"]) > .child')
$.find("span[email !='b@test.com'] .child");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.