不能在onclick函数中更改className JS。

问题描述 投票:0回答:1

做这个教程,但是当我在检查器调试器上运行时,content.className始终保持=""它只在指定的.onclick函数的设定过渡期内打开。所以该函数的效果只是暂时性的。

var button= document.getElementById("show-more");

button.onclick = () =>{
    //of the content has a class name of open then we open the box
    if(content.className== "open"){
        //if it's already opened then we shrink the box
        //revert to normal
        content.className= "";
        button.innerHTML= "Show More";
    }else{
        //else we expand the box
        //if it's closed we add open as it's class
        content.className= "open";
        button.innerHTML = "Show Less";

    }
}```
javascript scope classname
1个回答
0
投票

试试 content.classList.add("open"); 添加和 content.classList.remove("open"); 去掉

© www.soinside.com 2019 - 2024. All rights reserved.