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

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

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 -->

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

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

&lt;form&gt;
  &lt;input class=&quot;textfield&quot; type=&quot;text&quot; placeholder=&quot;RESULT&quot; name=&quot;text&quot; id=&quot;result&quot;&gt;

  &lt;input class=&quot;C&quot; type=&quot;reset&quot; value=&quot;AC&quot; title=&quot; All Clear&quot;&gt;
  &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;
  &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;
  &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;
  &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;
  &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;
  &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;
  &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;
  &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;
  &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;
  &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;
  &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;
  &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;
  &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;
  &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;
  &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;
  &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;
  &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;
  &lt;button type=&quot;button&quot; id=&quot;17&quot; class=&quot;button equals&quot; onclick=&quot;calc()&quot;&gt;=&lt;/button&gt;
  &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;
  &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;
&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()返回单个元素。

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

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

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

.darkbuttons {
  color: white;
  background-color: black;
}

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

&lt;form&gt;
  &lt;input class=&quot;textfield&quot; type=&quot;text&quot; placeholder=&quot;RESULT&quot; name=&quot;text&quot; id=&quot;result&quot;&gt;
  &lt;input class=&quot;C&quot; type=&quot;reset&quot; value=&quot;AC&quot; title=&quot; All Clear&quot;&gt;
  &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;
  &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;
  &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;
  &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;
  &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;
  &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;
&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:

确定