Changing the class of a div when clicking on it?

前端 未结 5 1511
时光说笑
时光说笑 2020-12-18 07:39

How can I change a div class name when clicking on it? Eg:

<
5条回答
  •  天命终不由人
    2020-12-18 07:42

    You have to use an if-statement to reverse it. Here's an example:

    function change_autorefreshdiv(){
        var NAME = document.getElementById("first_name")
        var currentClass = NAME.className;
        if(currentClass == "second_name"){
            NAME.className = "first_name";
        } else {
            NAME.className = "second_name";
        }
    } 
    

提交回复
热议问题