简体   繁体   English

等同于jquery中的.is()与本机javascript

[英]Equivalent of .is() in jquery to native javascript

I'm trying to convert my website JavaScript code from jQuery to native JavaScript because I want to learn JavaScript and also remove jQuery in my website. 我想将我的网站JavaScript代码从jQuery转换为本机JavaScript,因为我想学习JavaScript并在我的网站中删除jQuery。

This is the code im trying to convert 这是我试图转换的代码

 if ($('.product').is('.standalone, .expired')) { //results  }

is there an equivalent of this in JavaScript? JavaScript中有与此等效的东西吗?

Thank you in advance. 先感谢您。

Seems in this specific case you want to check if the element contains these two classes, if it is so then you can use document.querySelectorAll to get all the matched elements & check classList.contains to check if the element have that specific class 在这种特定情况下,您似乎要检查元素是否包含这两个类,如果是,则可以使用document.querySelectorAll获取所有匹配的元素并检查classList.contains以检查元素是否具有该特定类

 document.querySelectorAll('.product').forEach(function(item) { if (item.classList.contains('standalone') && item.classList.contains('expired')) { item.classList.add('colotText') } }) 
 .product { width: 100px; border: 1px solid red; } .colotText { color: green } 
 <div class="product standalone expired">One</div> <div class="product">Two</div> <div class="product standalone expired">Three</div> <div class="product">Four</div> 

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

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