简体   繁体   中英

How to toggle class by a button click?

How to toggle class by a button click?

How to remove a class on the next click from the same button?

  <button onclick="goldborder()" id="btn">Click here</button>

JS:

function goldborder() {
  let boxs = document.getElementById('box');
  boxs.classList.add('bordercolor');
}

You can try this. If the class exists it removes and if not it adds in it.

boxs.classList.toggle('bordercolor');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM