Change Class value With Javascript [duplicate]

落爺英雄遲暮 提交于 2020-12-13 07:20:03

问题


I want to make a button to change layout from wide to boxed, from class="container" to class="container-fluid", is it possible to change them by Javascript? If yes, how can i do that?


回答1:


Use document.getElementById("myEle").className to change the class. The classes in below code simply have the background colors. You can put your own css in place of them.

function changeClass(){
  document.getElementById("myEle").className = "container-fluid";
}
<style>
.container{
  background: yellow;
}
.container-fluid{
  background: red;
}
</style>
<div class="container" id="myEle">Hello World</div>
<button onClick="changeClass()">Change class</button>


来源:https://stackoverflow.com/questions/51430300/change-class-value-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!