Changing the class of a div when clicking on it?

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

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

<
5条回答
  •  再見小時候
    2020-12-18 08:08

    You have to define the second class name. Currently, you have got a function which changes the class name to a hard-coded value, independent on the current class name. See also: MDN: if...else

    function change_autorefreshdiv(){
        var NAME = document.getElementById("first_name");
        var currentClass = NAME.className;
        if (currentClass == "second_name") { // Check the current class name
            NAME.className = "first_name";   // Set other class name
        } else {
            NAME.className = "second_name";  // Otherwise, use `second_name`
        }
    }   
    

提交回复
热议问题