无法使用 JavaScript 中的 “document.querySelectorAll” 选择具有类 “.button” 的按钮。

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

Not able to select buttons with class ".button" with the help of "document.querySelectorAll" in javascript

问题

你为什么无法使用 querySelectorAll() 选择具有类名 .button 的按钮,尽管你可以使用 getElementById() 选择它们?这里你尝试改变你的HTML元素的CSS类。

英文:

Here is my current code:

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

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

  1. let variable = document.querySelectorAll(&quot;.button&quot;);
  2. variable.classList.toggle(&quot;darkbuttons&quot;);

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

  1. &lt;form&gt;
  2. &lt;input class=&quot;textfield&quot; type=&quot;text&quot; placeholder=&quot;RESULT&quot; name=&quot;text&quot; id=&quot;result&quot;&gt;
  3. &lt;input class=&quot;C&quot; type=&quot;reset&quot; value=&quot;AC&quot; title=&quot; All Clear&quot;&gt;
  4. &lt;button type=&quot;button&quot; class=&quot;adjust button&quot; title=&quot;clear&quot; onclick=&quot;text.value =text.value.toString().slice(0,-1)&quot;&gt;DE&lt;/button&gt;
  5. &lt;button type=&quot;button&quot; id=&quot;f&quot; class=&quot;button&quot; onclick=&quot;text.value += &#39;.&#39;&quot;&gt;.&lt;/button&gt;
  6. &lt;button type=&quot;button&quot; id=&quot;2&quot; class=&quot;button&quot; onclick=&quot;text.value += &#39;/&#39;&quot;&gt;/&lt;/button&gt;
  7. &lt;button type=&quot;button&quot; id=&quot;3&quot; class=&quot;button&quot; onclick=&quot;text.value += 1&quot;&gt;1&lt;/button&gt;
  8. &lt;button type=&quot;button&quot; id=&quot;4&quot; class=&quot;button&quot; onclick=&quot;text.value += 2&quot;&gt;2&lt;/button&gt;
  9. &lt;button type=&quot;button&quot; id=&quot;5&quot; class=&quot;button&quot; onclick=&quot;text.value += 3&quot;&gt;3&lt;/button&gt;
  10. &lt;button type=&quot;button&quot; id=&quot;6&quot; class=&quot;button&quot; onclick=&quot;text.value += &#39;+&#39;&quot;&gt;+&lt;/button&gt;
  11. &lt;button type=&quot;button&quot; id=&quot;7&quot; class=&quot;button&quot; onclick=&quot;text.value += 4&quot;&gt;4&lt;/button&gt;
  12. &lt;button type=&quot;button&quot; id=&quot;8&quot; class=&quot;button&quot; onclick=&quot;text.value += 5&quot;&gt;5&lt;/button&gt;
  13. &lt;button type=&quot;button&quot; id=&quot;9&quot; class=&quot;button&quot; onclick=&quot;text.value += 6&quot;&gt;6&lt;/button&gt;
  14. &lt;button type=&quot;button&quot; id=&quot;10&quot; class=&quot;button&quot; onclick=&quot;text.value += &#39;-&#39;&quot;&gt;-&lt;/button&gt;
  15. &lt;button type=&quot;button&quot; id=&quot;11&quot; class=&quot;button&quot; onclick=&quot;text.value += 7&quot;&gt;7&lt;/button&gt;
  16. &lt;button type=&quot;button&quot; id=&quot;12&quot; class=&quot;button&quot; onclick=&quot;text.value += 8&quot;&gt;8&lt;/button&gt;
  17. &lt;button type=&quot;button&quot; id=&quot;13&quot; class=&quot;button&quot; onclick=&quot;text.value += 9&quot;&gt;9&lt;/button&gt;
  18. &lt;button type=&quot;button&quot; id=&quot;14&quot; class=&quot;button&quot; onclick=&quot;text.value += &#39;*&#39;&quot;&gt;*&lt;/button&gt;
  19. &lt;button type=&quot;button&quot; id=&quot;15&quot; class=&quot;button&quot; onclick=&quot;text.value += &#39;00&#39;&quot;&gt;00&lt;/button&gt;
  20. &lt;button type=&quot;button&quot; id=&quot;16&quot; class=&quot;button&quot; onclick=&quot;text.value += 0&quot;&gt;0&lt;/button&gt;
  21. &lt;button type=&quot;button&quot; id=&quot;17&quot; class=&quot;button equals&quot; onclick=&quot;calc()&quot;&gt;=&lt;/button&gt;
  22. &lt;i class=&quot;fa-solid fa-circle-half-stroke&quot; onclick=&quot;change()&quot; id=&quot;fas&quot; title=&quot;switch to dark mode&quot;&gt;Darkmode&lt;/i&gt;
  23. &lt;i class=&quot;fa-brands fa-linkedin&quot; id=&quot;fab&quot; title=&quot;visit profile&quot;&gt; &lt;a href=&quot;https://www.linkedin.com/in/ayush-sharma-a155a8267&quot;&gt; Linked in&lt;/a&gt; &lt;/i&gt;
  24. &lt;/form&gt;

<!-- end snippet -->

Why am I not able to select the buttons with class .button with querySelectorAll(), though I am able to select them with getElementById(); here I am trying to change the CSS class of my html elements.

答案1

得分: 1

你需要循环遍历每个按钮,因为document.querySelectorAll()返回一个元素数组。
getElementById()返回单个元素。

  1. let buttons = document.querySelectorAll(".button");
  2. buttons.forEach(button => {
  3. button.classList.toggle("darkbuttons");
  4. });
  1. .darkbuttons {
  2. color: white;
  3. background-color: black;
  4. }
  1. <form>
  2. <input class="textfield" type="text" placeholder="RESULT" name="text" id="result">
  3. <input class="C" type="reset" value="AC" title=" All Clear">
  4. <button type="button" class="adjust button" title="clear" onclick="text.value = text.value.toString().slice(0,-1)">DE</button>
  5. <button type="button" id="f" class="button" onclick="text.value += '.'">.</button>
  6. <button type="button" id="2" class="button" onclick="text.value += '/'">/</button>
  7. <button type="button" id="3" class="button" onclick="text.value += 1">1</button>
  8. <button type="button" id="4" class="button" onclick="text.value += 2">2</button>
  9. <button type="button" id="5" class="button" onclick="text.value += 3">3</button>
  10. </form>
英文:

You need to loop through each button, because document.querySelectorAll() returns an array of elements.
getElementById() returns a single element.

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

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

  1. let buttons = document.querySelectorAll(&quot;.button&quot;);
  2. buttons.forEach(button =&gt; {
  3. button.classList.toggle(&quot;darkbuttons&quot;);
  4. });

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

  1. .darkbuttons {
  2. color: white;
  3. background-color: black;
  4. }

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

  1. &lt;form&gt;
  2. &lt;input class=&quot;textfield&quot; type=&quot;text&quot; placeholder=&quot;RESULT&quot; name=&quot;text&quot; id=&quot;result&quot;&gt;
  3. &lt;input class=&quot;C&quot; type=&quot;reset&quot; value=&quot;AC&quot; title=&quot; All Clear&quot;&gt;
  4. &lt;button type=&quot;button&quot; class=&quot;adjust button&quot; title=&quot;clear&quot; onclick=&quot;text.value =text.value.toString().slice(0,-1)&quot;&gt;DE&lt;/button&gt;
  5. &lt;button type=&quot;button&quot; id=&quot;f&quot; class=&quot;button&quot; onclick=&quot;text.value += &#39;.&#39;&quot;&gt;.&lt;/button&gt;
  6. &lt;button type=&quot;button&quot; id=&quot;2&quot; class=&quot;button&quot; onclick=&quot;text.value += &#39;/&#39;&quot;&gt;/&lt;/button&gt;
  7. &lt;button type=&quot;button&quot; id=&quot;3&quot; class=&quot;button&quot; onclick=&quot;text.value += 1&quot;&gt;1&lt;/button&gt;
  8. &lt;button type=&quot;button&quot; id=&quot;4&quot; class=&quot;button&quot; onclick=&quot;text.value += 2&quot;&gt;2&lt;/button&gt;
  9. &lt;button type=&quot;button&quot; id=&quot;5&quot; class=&quot;button&quot; onclick=&quot;text.value += 3&quot;&gt;3&lt;/button&gt;
  10. &lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月20日 23:05:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295886.html
匿名

发表评论

匿名网友

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

确定