如何使用类更改 div 内所有按钮的 tabindex

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

How to change tabindex of all button inside a div using class

问题

I want to set tabindex for all button inside a div my structure is like this.

<div class="content">
  <div> <button name="first" /> </div>
  <div> <button name="second" /> </div>
  <div> <button name="third" /> </div>
<div>
I am trying it with below code:

```javascript
const content = document.getElementsByClassName("content");
const kbButtons = content[0].getElementsByTagName("button");
kbButtons.map( (elem,index) => { elem.setAttribute("tabindex", '-1') })
英文:

I want to set tabindex for all button inside a div my structure is like this.

&lt;div class=&quot;content&quot;&gt;
  &lt;div&gt; &lt;button name=&quot;first&quot; /&gt; &lt;/div&gt;
  &lt;div&gt; &lt;button name=&quot;second&quot; /&gt; &lt;/div&gt;
  &lt;div&gt; &lt;button name=&quot;third&quot; /&gt; &lt;/div&gt;
&lt;div&gt;

I am trying it with below code:

const content = document.getElementsByClassName(&quot;content&quot;);
const kbButtons = content[0].getElementsByTagName(&quot;button&quot;);
kbButtons.map( (elem,index) =&gt; { elem.setAttribute(&quot;tabindex&quot;, &#39;-1&#39;) })  

答案1

得分: 3

The Element.getElementsByTagName() method returns an HTMLCollection, and not an array. Since it's not an array, it doesn't have the Array.map() method. In addition, map is used to transform an array to a new array, not for side effects (this is the purpose of forEach()).

使用 Document.querySelectorAll() 来获取所有在 .content 下的按钮的 NodeList,然后使用 NodeList.forEach() 迭代它们,并设置 tab 键索引:

const kbButtons = document.querySelectorAll('.content button')
  .forEach(elem => {
    elem.setAttribute('tabindex', '-1')
  })
<div class="content">
  <div><button name="first">first</button></div>
  <div><button name="second">second</button></div>
  <div><button name="third">third</button></div>
</div>
英文:

The Element.getElementsByTagName() method returns an HTMLCollection, and not an array. Since it's not an array, it doesn't have the Array.map() method. In addition, map is used to transform an array to a new array, not for side effects (this is the purpose of forEach()).

Use Document.querySelectorAll() to get an NodeList of all buttons which are descendants of .content, and then iterate them with NodeList.forEach(), and set the tab index:

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

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

const kbButtons = document.querySelectorAll(&#39;.content button&#39;)
  .forEach(elem =&gt; {
    elem.setAttribute(&#39;tabindex&#39;, &#39;-1&#39;)
  })

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

&lt;div class=&quot;content&quot;&gt;
  &lt;div&gt;&lt;button name=&quot;first&quot;&gt;first&lt;/button&gt;&lt;/div&gt;
  &lt;div&gt;&lt;button name=&quot;second&quot;&gt;second&lt;/button&gt;&lt;/div&gt;
  &lt;div&gt;&lt;button name=&quot;third&quot;&gt;third&lt;/button&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 2

You can use querySelectorAll():

const btns = document.querySelectorAll(".content button");
for (var i = 0; i < btns.length; i++) {
  btns[i].setAttribute("tabindex", '-1');
}
<div class="content">
  <div> <button name="first" /> </div>
  <div> <button name="second" /> </div>
  <div> <button name="third" /> </div>
<div>
英文:

You can use querySelectorAll():

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

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

const btns = document.querySelectorAll(&quot;.content button&quot;);
for(var i=0; i&lt;btns.length; i++) {
  btns[i].setAttribute(&quot;tabindex&quot;, &#39;-1&#39;);
}

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

&lt;div class=&quot;content&quot;&gt;
  &lt;div&gt; &lt;button name=&quot;first&quot; /&gt; &lt;/div&gt;
  &lt;div&gt; &lt;button name=&quot;second&quot; /&gt; &lt;/div&gt;
  &lt;div&gt; &lt;button name=&quot;third&quot; /&gt; &lt;/div&gt;
&lt;div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 17:21:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575910.html
匿名

发表评论

匿名网友

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

确定