[英]How to find if an element contains subelement with id='x' using jQuery?
Let's say I've got the following HTML: 假设我有以下HTML:
<div class"my-class">
<div id="something">
blah
</div>
</div>
<div class"my-class">
<div id="something-else">
<a href="/" id="x">bluh</a>
</div>
</div>
When I loop over all elements of class .my-class
I want to find the one that contains a sub-element (or sub-sub-element like in my example above) with id #x
. 当我遍历类
.my-class
所有元素时,我想找到一个包含ID为#x
的子元素(或上面的示例中的子元素)的元素。 So in the example above I want to find the second element with class .my-class
. 因此,在上面的示例中,我想找到类为
.my-class
的第二个元素。
So something like this: 所以像这样:
$(".my-class").each(function() {
if ($(this).containsSubElementWithId("#x")){ // <= HOW CAN I DO THIS?
// Do something with $(this)
}
});
Does anybody know how I can test whether an element contains another element with a certain id? 有人知道我如何测试一个元素是否包含另一个具有特定ID的元素吗? All tips are welcome!
欢迎所有提示!
There is an even simpler way: 有一个更简单的方法:
var matches = $(".my-class:has(#x)");
// Do something with the matching elements
JSFiddle: http://jsfiddle.net/TrueBlueAussie/kj1wzytw/ JSFiddle: http : //jsfiddle.net/TrueBlueAussie/kj1wzytw/
As you are looking for an ID-based element, which can only have a single match, the "fastest" search would be an ID search, followed by closest
: 当您寻找一个只能有一个匹配项的基于ID的元素时,“最快”的搜索将是ID搜索,然后是“
closest
:
eg 例如
var matches = $('#x').closest('.my-class');
but if you have multiple matches possible, (class or ID prefix etc), go with the first way. 但是如果您可能有多个匹配项(类或ID前缀等),请采用第一种方法。
eg (ids starting starting with x
): 例如(以
x
开头的id):
var matches = $(".my-class:has([id^=x])");
JSFiddle: http://jsfiddle.net/TrueBlueAussie/kj1wzytw/2/ JSFiddle: http : //jsfiddle.net/TrueBlueAussie/kj1wzytw/2/
generally a class search is almost as fast as an ID search, on modern browsers, so you would be better off using a class for multiple matches. 通常,在现代浏览器中,类搜索几乎与ID搜索一样快,因此最好将一个类用于多个匹配项。
eg 例如
var matches = $(".my-class:has(.x)");
JSFiddle: http://jsfiddle.net/TrueBlueAussie/kj1wzytw/3/ JSFiddle: http : //jsfiddle.net/TrueBlueAussie/kj1wzytw/3/
You can use the has()
method: 您可以使用
has()
方法:
$(".my-class").each(function() {
if ($(this).has("#x").length){
// Do something with $(this)
}
});
$(this).find('#x').length
will also work. $(this).find('#x').length
也可以工作。
Try using each()
function and check the length of the specific element: 尝试使用
each()
函数并检查特定元素的长度:
var func = function() { $('.my-class').each(function() { var that = $(this); if (that.find('#x').length > 0) { console.log('found!'); // that.something()... } }); } func();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="my-class"> <div id="something"> blah </div> </div> <div class="my-class"> <div id="something-else"> <a href="/" id="x">bluh</a> </div> </div>
Try with - 尝试-
$(this).find("[id='x']");
For checking it - 为了检查它-
$(this).find("[id='x']").length;
Try this: 尝试这个:
var myClassElement = $(".my-class #x").parents('.my-class');
// Do something with myClassElement
If you want to do something with .my-class element event if it doesn't contain #x element use this: 如果您想对.my-class元素事件执行某些操作(如果它不包含#x元素),请使用以下命令:
$(".my-class").each(function() {
var containsXElement = $(this).find("#x").length > 0;
if (containsXElement){
// Do something with $(this)
}
});
$(".my-class").each(function() {
if ($(this).find("a[id=x]")){
//do whatever you want.
}
});
如果只想定位具有该元素的div,我建议您先找到该元素,然后遍历到具有所需类的最接近的父div:
var objhasx = $('#x').closest('.my-class');
Following your initial technique: 遵循您的初始技术:
$(".my-class").each(function() {
var $this = $(this);
// method 1
if ($this.find("#x").length > 0) {
// use $this
}
// method 2
if ($this.find("[id='x']").length > 0) {
// use $this
}
});
More @ 更多 @
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.