简体   繁体   English

查找元素是否包含在jquery选择器中

[英]Find whether an element contains in a jquery selector

Let say I have, 我想说,我有,

<a id="a123"></a>

Now I need to check whether $("a") selector contains $("#a123") element. 现在,我需要检查$(“ a”)选择器是否包含$(“#a123”)元素。

If you want to find whether a has this a123 id then you can do it using is method which returns true if it matches the selector else returns false. 如果要查找a是否具有此a123 id,则可以使用is方法(如果它与选择器匹配,则返回true ,否则返回false。

if($("a").is('#a123')){
     //It is #a123
}

If you want to find if a contains child with id a123 then use has method which reduces the set of matched elements to those that have a descendant that matches the selector or DOM element.. 如果要查找是否包含ID为a123 a包含子项,请使用has方法将匹配元素集减少为后代与选择器或DOM元素匹配的元素。

if($("a").has('#a123').length > 0){
     //It contains child with #a123
}

References: 参考文献:

$("a").filter("#a123").size() > 0

You can use.attr("id") to pull back the value of the id. 您可以使用.attr(“ id”)来拉回id的值。 Then you can use an if statement to check if the value is what you expected. 然后,您可以使用if语句来检查该值是否是您期望的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM