为所有包含特定文本的按钮添加类。

huangapple go评论69阅读模式
英文:

Add class to all buttons with certain text

问题

I;m trying to figure out how to add an class to all buttons with a certain text. I've got the below code working but it's only working for one button:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>
    var isActive = document.querySelector('.btn-with-text');

    if (isActive.innerText.trim() === 'MY Custom Text') {
    document.querySelector('.btn-with-text').classList.add('btn-extra-class');
    }
</script>

As far as my onderstanding I should use querySelectorAll and a forEach statement, but I cannot seem to get it work.

英文:

I;m trying to figure out how to add an class to all buttons with a certain text. I've got the below code working but it's only working for one button:

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;script&gt;
    var isActive = document.querySelector(&#39;.btn-with-text&#39;);

    if (isActive.innerText.trim() === &#39;MY Custom Text&#39;) {
    document.querySelector(&#39;.btn-with-text&#39;).classList.add(&#39;btn-extra-class&#39;);
    }
&lt;/script&gt;

As far as my onderstanding I should use querySelectorAll and a forEach statement, but I cannot seem to get it work.

答案1

得分: 1

是的,您需要使用 querySelectorAll 并循环遍历数组中的所有元素。以下是如何实现的:

// 获取所有具有所需类的按钮
var buttonsWithMyText = document.querySelectorAll('.btn-with-text');

// 循环遍历所有按钮
buttonsWithMyText.forEach(button => {
  // 检查按钮内的文本是否与您搜索的相符
  if (button.innerText !== 'MY Custom Text') return;
  
  // 添加您想要的附加类
  button.classList.add('btn-extra-class');
});
<!-- 适用于任何类型的标签 -->
<a href="#" class="btn-with-text">MY Custom Text</a>
<a href="#" class="btn-with-text">OTHER Custom Text</a>
<a href="#" class="btn-with-text-different">MY Custom Text</a>
<br>
<button class="btn-with-text">MY Custom Text</button>
<button class="btn-with-text">OTHER Custom Text</button>
<button class="btn-with-text-different">MY Custom Text</button>
/* CSS 仅用于演示 - 不是必需的 */

a, button
{
  display: block;
}

.btn-with-text,
.btn-with-text-different
{
  color: white;
}

.btn-extra-class
{
  color: red;
}

希望这有所帮助!

英文:

Yes, you need to use querySelectorAll and loop through all elements in the array. Here is how that could happen:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Get all buttons with the class you want
var buttonsWithMyText = document.querySelectorAll(&#39;.btn-with-text&#39;);

// Loop through all buttons
buttonsWithMyText.forEach(button =&gt; {
  // Check if the text inside the button is what you search for
  if (button.innerText !== &#39;MY Custom Text&#39;) return;
  
  // Add the additional class you want 
  button.classList.add(&#39;btn-extra-class&#39;);
});

<!-- language: lang-html -->

&lt;!-- Works with any kind of tag --&gt;
&lt;a href=&quot;#&quot; class=&quot;btn-with-text&quot;&gt;MY Custom Text&lt;/a&gt;
&lt;a href=&quot;#&quot; class=&quot;btn-with-text&quot;&gt;OTHER Custom Text&lt;/a&gt;
&lt;a href=&quot;#&quot; class=&quot;btn-with-text-different&quot;&gt;MY Custom Text&lt;/a&gt;
&lt;br&gt;
&lt;button class=&quot;btn-with-text&quot;&gt;MY Custom Text&lt;/button&gt;
&lt;button class=&quot;btn-with-text&quot;&gt;OTHER Custom Text&lt;/button&gt;
&lt;button class=&quot;btn-with-text-different&quot;&gt;MY Custom Text&lt;/button&gt;

<!-- language: lang-css -->

/* CSS is just to showcase - it is not needed */

a, button
{
  display: block;
}

.btn-with-text,
.btn-with-text-different
{
  color: white;
}

.btn-extra-class
{
  color: red;
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月29日 19:10:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定