简体   繁体   English

如何判断元素是否与选择器匹配?

[英]How can I tell whether an element matches a selector?

Let's say I've got a DOM element - how can I tell whether it matches a jQuery selector, such as p or .myclass ?假设我有一个 DOM 元素 - 我如何判断它是否匹配 jQuery 选择器,例如p.myclass It's easy to use the selector to match children of the element, but I want a true/false answer to whether this particular element match?使用选择器来匹配元素的子元素很容易,但我想要这个特定元素是否匹配的真/假答案?

The element may not have an ID (and I can't assign it a random one for reasons beyond this), so I can't apply my selector to the element's parent and look for children with the same ID as mine.该元素可能没有 ID(出于其他原因,我无法为其分配一个随机 ID),因此我无法将选择器应用于元素的父级并查找与我的 ID 相同的子级。

Will this work as intended?这会按预期工作吗? I can't figure out Javascript object comparisons.我无法弄清楚 Javascript 对象比较。

$(selector, myElement.parentNode).each({
  if (this == myElement) // Found it
});

Seems like there would be an easy way to see whether a DOM element matches a jQuery selector...似乎有一种简单的方法可以查看 DOM 元素是否与 jQuery 选择器匹配......

You can use the is() method:您可以使用is()方法:

if($(this).is("p")){
 // ...
}

Without jQuery, using Element.matches() :没有 jQuery,使用Element.matches()

 var elements = document.querySelectorAll('div'); console.log( elements[0].matches('.foo'), // true elements[0].matches('.bar'), // false elements[1].matches('[title=bar]'), // true )
 <div class='foo'></div> <div title='bar'></div>


See supported browsers list查看支持的浏览器列表

I believe the is() method is what you are looking for.我相信is()方法是您正在寻找的。

Otherwise, you might just try selecting the item directly, if that is what you mean.否则,您可能只是尝试直接选择该项目,如果这是您的意思。

So if it's an "a" with a class of "myclass", just do $("a.myclass")因此,如果它是一个带有“myclass”类的“a”,只需执行 $("a.myclass")

I can't apply my selector to the element's parent and look for children with the same ID as mine.我无法将我的选择器应用于元素的父元素并寻找与我的 ID 相同的子元素。

You can't do that anyway - the id attribute must be unique.无论如何您都不能这样做 - id属性必须是唯一的。

Maybe you just want:也许你只是想要:

$(".complicated #selector p.with > div.manyParts").length

If there are no matches, length returns 0, which is falsy, so you can use it in an if statement, like:如果没有匹配,长度返回 0,这是假的,所以你可以在if语句中使用它,比如:

if($(".doesThis #exist").length) {
    // Do things
}

Probably would be worth storing it in a separate variable and THEN checking the length;可能值得将它存储在一个单独的变量中,然后检查长度; that way, if you need to do anything with the matched elements, you don't have to execute the same selector again.这样,如果您需要对匹配的元素做任何事情,您不必再次执行相同的选择器。

Try Element.matches()试试Element.matches()

Element.matches() 元素匹配()

document.addEventListener("click", event => {
  if (event.target.matches(".elementsClass")) {
    // It matches
  } else {
    // Does not match
  }
});

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

相关问题 如何判断数组中的元素是数字还是单词(引号中的所有元素) - How can I tell whether or not an element inside an array is a number or a word (all elements in quotes) 如何判断“我的页面上是否存在 Javascript object”? - How can I tell 'Whether a Javascript object exists on my page'? 如何判断ReactJS是否处于JavaScript开发模式? - How can I tell whether ReactJS is in development mode from JavaScript? 我如何判断一个元素是否在影子 DOM 中? - How can I tell if an element is in a shadow DOM? 如何通过Chrome选择器获取元素? - How i can get element by selector of chrome? 如何判断一个元素是 jquery DOM 元素还是原版? - how can I tell if an element is a jquery DOM element or vanilla? 如何判断&#39;font-family&#39;请求是否得到尊重,以便我可以在必要时替换图像? - How can I tell whether a 'font-family' request was honoured so that I can substitute an image if necessary? 如何检查元素是否可以托管 Shadow DOM? - How can i check whether an element can host Shadow DOM? 如何判断我的元素是否需要innerHTML或值 - How to tell whether my element needs innerHTML or value 如何让函数判断是否应用“this”或其他元素 ID - How to have a function tell whether to apply on "this" or other element ID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM