如何检查元素classlist是否部分包含字符串或单词?

发布于 2025-02-07 03:15:49 字数 673 浏览 2 评论 0原文

我有这个代码:

const all = document.querySelectorAll('div');
let variable = 'fruits';

all.forEach((item) => {
  if(item.classList.contains(variable)) {
   item.style.backgroundColor = 'red';
  }
});
<div class="fruits">
  Fruits
</div>

<div class="fruits-green">
  Green fruits
</div>

我想知道是否有classList.contains用字符串/单词部分使用?

显然,我要实现的目标也是要使水果绿色造型,因为它包含水果 green,

我知道有CSS选择器为此,但是鉴于我的水果变量会随其他JS而变化。功能,我也希望这也是香草JS。

任何建议都非常感谢。 谢谢!

I've got this code:

const all = document.querySelectorAll('div');
let variable = 'fruits';

all.forEach((item) => {
  if(item.classList.contains(variable)) {
   item.style.backgroundColor = 'red';
  }
});
<div class="fruits">
  Fruits
</div>

<div class="fruits-green">
  Green fruits
</div>

and I was wondering if theres to use classList.contains with a string/word partially?

Obviously what I'm trying to achieve is to get fruits-green styled too, since it contains fruits-green

I know there are CSS selectors for that, but given that my fruits variable will change with other JS functions, I'd like this to be vanilla JS too.

Any advice is much appreciated.
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

各自安好 2025-02-14 03:15:50

我能够使用

let variable = 'fruits';
const all = document.querySelectorAll('div[class*="'+variable+'"]');

all.forEach((item) => {
  item.style.backgroundColor = 'red';
});
<div class="fruits">
  Fruits
</div>

<div class="fruits-green">
  Green fruits
</div>

再次感谢@david

I was able to make it work using

let variable = 'fruits';
const all = document.querySelectorAll('div[class*="'+variable+'"]');

all.forEach((item) => {
  item.style.backgroundColor = 'red';
});
<div class="fruits">
  Fruits
</div>

<div class="fruits-green">
  Green fruits
</div>

Thanks again @David

煞人兵器 2025-02-14 03:15:49

classList属性是domtokenlist,它是字符串,但具有模拟数组方法的方法和属性。

  • 通过标签数组迭代(ash all)。我们将使用.flatmap(),因为我们正在处理可能具有或可能没有多个匹配的多个标签。
      all.flatmap(((div,idx)=&gt; {// ...
     
  • 接下来,我们将使用.value classList的属性,该属性返回标签的每个类的空间划界字符串。
     让list = div.classlist.value.split('');
     
  • 然后,我们将通过与.includes()进行比较,我们将.filter()每个标签的每个类
      list.filter(cls =&gt; cls.tolowercase()。include('fruit'));
     
  • 最后,返回了一个数组。每个子阵列都包含标签的匹配类。
      const all = array.from(document.queryselectorall('div''));
    
    令输出= all.flatmap(((div,idx)=&gt; {
      令list = div.classlist.value.split('');
      console.log(列表)
      令匹配= list.filter(cls =&gt; cls.tolowercase()。包括('fruit'));
      返回[匹配];
    }))
    
    console.log(输出);  
     &lt; div class =“ fruits frues fries”&gt;
      水果薯条
    &lt;/div&gt;
    
    &lt; div class =“水果 - 绿色果味”&gt;
      绿色水果水果
    &lt;/div&gt;  

The classList property is a DOMTokenList which is a string but has methods and properties simular to Array methods.

  • Iterate through the array of tags (all). We'll use .flatMap() because we are dealing with multiple tags that may or may not have multiple matches.
    all.flatMap((div, idx) => {//...
    
  • Next, we'll use the .value property of classList which returns a space delimited string of each of the tag's classes.
    let list = div.classList.value.split(' ');
    
  • Then we'll .filter() each class of each tag by comparing with .includes()
    list.filter(cls => cls.toLowerCase().includes('fruit'));
    
  • Finally, an array of arrays is returned. Each sub-array contains a tag's matching classes.
    const all = Array.from(document.querySelectorAll('div'));
    
    let output = all.flatMap((div, idx) => {
      let list = div.classList.value.split(' ');
      console.log(list)
      let matches = list.filter(cls => cls.toLowerCase().includes('fruit'));
      return [matches];
    })
    
    console.log(output);
    <div class="fruits fries">
      Fruits fries
    </div>
    
    <div class="fruits-green fruity">
      Green fruits fruit
    </div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文