Java Script - 用条件替换文本

有一个清单。如果文本为“00”,我想将文本更改为“0”不起作用。


let turnToZero = document.getElementsByClassName("tutorial_length");

for (var i = 0; i < turnToZero.length; i++){

  let x = turnToZero[i].textContent;

  if ( x = "00" ) { x ="0"} 

}

<ul>

   <li class="tutorial_length">40</li>

   <li class="tutorial_length">00</li>

</ul>


胡说叔叔
浏览 98回答 1
1回答

胡子哥哥

您的代码中有两个问题:您正在使用赋值 (&nbsp;=) 运算符而不是比较 (&nbsp;==or&nbsp;===) 运算符。您必须更新元素textcontent属性。改变:if&nbsp;(&nbsp;x&nbsp;=&nbsp;"00"&nbsp;)&nbsp;{&nbsp;x&nbsp;="0"}至:if&nbsp;(&nbsp;x&nbsp;==&nbsp;"00"&nbsp;)&nbsp;{&nbsp;turnToZero[i].textContent&nbsp;="0";&nbsp;}let turnToZero = document.getElementsByClassName("tutorial_length");for (var i = 0; i < turnToZero.length; i++){&nbsp; let x = turnToZero[i].textContent;&nbsp; if ( x == "00" ) { turnToZero[i].textContent = "0"; }&nbsp;}<ul>&nbsp; <li class="tutorial_length">40</li>&nbsp; <li class="tutorial_length">00</li></ul>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript