gpt4 book ai didi

Javascript classList.remove 无法正常工作

转载 作者:数据小太阳 更新时间:2023-10-29 05:43:58 25 4
gpt4 key购买 nike

检查这个 fiddle :JSFiddle

HTML:

<table class="myTable">
<tr>
<th>Some text</th>
<td class="align">
<span class=" someStyle">1</span>/<span class="otherStyle">15</span>
</td>
<th>Some text</th>
<td class="align">
<span class=" someStyle">2</span>/<span class="otherStyle">16</span>
</td>
</tr>
<tr>
<th>Some text</th>
<td class="align">
<span class="">3</span>/<span class="">17</span>
</td>
<th>Some text</th>
<td class="align">
<span class="otherStyle">4</span>/<span class=" someStyle">18</span>
</td>
</tr>
<tr>
<th>Some text</th>
<td class="align">
<span class="otherStyle">5</span>/<span class=" someStyle">19</span>
</td>
<th>Some text</th>
<td class="align">
<span class="">6</span>/<span class="">20</span>
</td>
</tr>
<tr>
<th>Some text</th>
<td class="align">
<span class="">7</span>/<span class="">21</span>
</td>
<th>Some text</th>
<td class="align">
<span class=" someStyle">8</span>/<span class="otherStyle">22</span>
</td>
</tr>
<tr>
<th>Some text</th>
<td class="align">
<span class="">9</span>/<span class="">23</span>
</td>
<th>Some text</th>
<td class="align">
<span class="otherStyle">10</span>/<span class=" someStyle">24</span>
</td>
</tr>
<tr>
<th>Some text</th>
<td class="align">
<span class="otherStyle">11</span>/<span class=" someStyle">25</span>
</td>
<th>Some text</th>
<td class="align">
<span class="otherStyle">12</span>/<span class=" someStyle">26</span>
</td>
</tr>
<tr>
<th>Some text</th>
<td class="align">
<span class=" someStyle">13</span>/<span class="otherStyle">27</span>
</td>
<th>Some text</th>
<td class="align">
<span class=" someStyle">14</span>/<span class="otherStyle">28</span>
</td>
</tr>
</table>

JavaScript:

var myTbl = document.getElementsByClassName("myTable")[0];

var tSomeStyleClasses = myTbl.getElementsByClassName("someStyle");
console.log(tSomeStyleClasses);
for (i=0;i<tSomeStyleClasses.length;i++) {
console.log(tSomeStyleClasses[i].classList);
//tSomeStyleClasses[i].classList.remove("someStyle");
}

var tOtherStyleClasses = myTbl.getElementsByClassName("otherStyle");
console.log(tOtherStyleClasses);
for (i=0;i<tOtherStyleClasses.length;i++) {
console.log(tOtherStyleClasses[i].classList);
//tOtherStyleClasses[i].classList.remove("otherStyle");
}

并检查控制台日志。每个有 10 个条目,someStyle 和 otherStyle。现在取消注释 //tSomeStyleClasses[i].classList.remove("someStyle");//tOtherStyleClasses[i].classList.remove("otherStyle"); 和运行 fiddle 。再次检查控制台日志。应该删除 2 x 10 个样式,但它只删除了 5 个样式。我想知道为什么?

最佳答案

.getElementsByClassName() 返回的值是一个事件 NodeList。它是“实时的”意味着当您更改列表中的元素时,列表本身会自动更新。因此,当您删除 用于查找元素的类时,列表会变短。因为您正在使用数字索引进行迭代,所以您最终会跳过元素。

处理这个问题的一个好方法是使用一个简单的 while 循环,只对列表的第一个元素进行操作,直到列表为空:

var tSomeStyleClasses = myTbl.getElementsByClassName("someStyle");

while (tSomeStyleClasses.length) {
tSomeStyleClasses[0].classList.remove("someStyle");
}

关于Javascript classList.remove 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42610369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com