zoukankan      html  css  js  c++  java
  • js添加和删除class

    原生主要有三种方法:

    1、className

    var DomClass = document.getElementById("id").className;
    //删除 pat
    DomClass = DomClass.replace('pat',"");
    document.getElementById("id").className = DomClass;
    
    //添加 pad
    DomClass = DomClass.concat('pat');
    document.getElementById("id").className = DomClass;
    
    //替换 pad
    DomClass = DomClass.replace('pat',"pad");
    document.getElementById("id").className = DomClass;

    2、es5的classList

    var DomClass = document.getElementById("id");
    //删除 pat
    DomClass.classList.remove("pat");
    
    //添加 pad
    DomClass.classList.add("pat");

    3、同一 setAttribute

    var DomClass = document.getElementById("id").getAttribute('class');
    //删除 pat
    DomClass = DomClass.replace('pat',"");
    document.getElementById("id").setAttribute('class',DomClass);
    
    //添加 pad
    DomClass = DomClass.concat('pat');
    document.getElementById("id").setAttribute('class',DomClass);
    
    //替换 pad
    DomClass = DomClass.replace('pat',"pad");
    document.getElementById("id").setAttribute('class',DomClass);

    @

  • 相关阅读:
    PHP调试总结
    vim常用命令
    Xshell
    JavaScript
    HTML+CSS
    解决VMware“该虚拟机似乎正在使用中”问题
    MVC dirname(——FILE——)
    各种编程语言中的指针和引用
    Go defer 原理和源码剖析
    软件架构定义的流派
  • 原文地址:https://www.cnblogs.com/change-oneself/p/10339315.html
Copyright © 2011-2022 走看看