檢查此琴:JSFiddle的Javascript classList.remove不能正常工作
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");
並運行小提琴。再次檢查控制檯日誌。應該刪除2 x 10個樣式,但是隻能刪除5個樣式。 我想知道爲什麼?
@ T.J。 Crowder和Pointy,謝謝你幫助我。 – Tomo