This question already has answers here:
addClass and removeClass in jQuery - not removing class
                                
                                    (10个回答)
                                
                        
                                2年前关闭。
            
                    
我在替换类时遇到问题,我想使用jquery或java脚本替换类。我需要更改ul标签内的类。我该如何改变

 <ul  id="user-listview" class="userlist" data-bind="foreach:Items">
    <li>
   <span id="spname" data-bind="text:userName"  class="c-user"/><span>
   <div>
      <img id="imguser" data-bind="attr: { src:userImageUrl }" class="c-image" />
     </div>
   </li>
  </ul>

 // I am trying like below code but its doesn't work
  $(".userlist li #spname").removeClass('c-user').addClass('IPuser');
  $(".userlist li #imguser").removeClass('c-image').addClass('IPimage');

最佳答案

window.document.getElementById("myId").classList.add("myNewClass");


如果要删除上一个类:

window.document.getElementById("myId").classList.remove("myOldClass");

关于javascript - 用新类(class)替换类(class),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43660142/

10-09 22:42