最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Click button to add a css style with js - Stack Overflow

programmeradmin0浏览0评论

What I want to achieve is to click one button and then the .clicked class will be added to the button I am clicking. But I also want to remove the class when I click one of the other buttons.

<div>
  <button class="btn-color-black btn">Yo!</button>
  <button class="btn-color-black btn">Yo!</button>
  <button class="btn-color-black btn">Yo!</button>
</div>

Css:

.clicked {
  color: pink;
}

Javascript

var btn = document.getElementsByClassName("btn");

for (var i = 0; i < btn.length; i++) {
    btn[i].addEventListener("click", myFunction);
}

function myFunction() {
    var parentElement = this.parentElement;

    if (this.classList.length <= 2) {
    this.classList.add("clicked");

  } else {
    this.classList.remove("clicked");

  }
}

/

What I want to achieve is to click one button and then the .clicked class will be added to the button I am clicking. But I also want to remove the class when I click one of the other buttons.

<div>
  <button class="btn-color-black btn">Yo!</button>
  <button class="btn-color-black btn">Yo!</button>
  <button class="btn-color-black btn">Yo!</button>
</div>

Css:

.clicked {
  color: pink;
}

Javascript

var btn = document.getElementsByClassName("btn");

for (var i = 0; i < btn.length; i++) {
    btn[i].addEventListener("click", myFunction);
}

function myFunction() {
    var parentElement = this.parentElement;

    if (this.classList.length <= 2) {
    this.classList.add("clicked");

  } else {
    this.classList.remove("clicked");

  }
}

https://jsfiddle/saj9oxyv/7/

Share Improve this question asked Feb 6, 2018 at 22:29 Eirik VattøyEirik Vattøy 1751 gold badge2 silver badges14 bronze badges 1
  • So what is your question? – connexo Commented Feb 6, 2018 at 22:31
Add a ment  | 

5 Answers 5

Reset to default 3

What you can do is use querySelector to grab the current "clicked" element, remove the class, and then add the class to the clicked element.

var btn = document.getElementsByClassName("btn");

for (var i = 0; i < btn.length; i++) {
    btn[i].addEventListener("click", myFunction);
}

function myFunction() {
    var parentElement = this.parentElement;
    var previousElement = document.querySelector('.clicked');

    if (previousElement) {
        previousElement.classList.remove('clicked');
    }

    if (this.classList.length <= 2) {
    this.classList.add("clicked");

  } else {
    this.classList.remove("clicked");

  }
}
.clicked {
  color: pink;
}
<div>
  <button class="btn-color-black btn">Yo!</button>
  <button class="btn-color-black btn">Yo!</button>
  <button class="btn-color-black btn">Yo!</button>
</div>

You can "remember" which one was clicked:

var lastBtn;
var btn = document.getElementsByClassName("btn");

for (var i = 0; i < btn.length; i++) {
    btn[i].addEventListener("click", myFunction);
}

function myFunction() {
  if (lastBtn)
    lastBtn.classList.remove("clicked");
  
  this.classList.add("clicked");
  
  lastBtn = this;
}
.clicked {
  color: pink;
}
<div>
  <button class="btn-color-black btn">Yo!</button>
  <button class="btn-color-black btn">Yo!</button>
  <button class="btn-color-black btn">Yo!</button>
</div>

Here is a concise solution. On click, it first removes .clicked from the button collection. Then it adds the class to the clicked button (which is available as event.target, the event (object) that triggered the function is always passed to the event handler automatically).

var btn = document.getElementsByClassName("btn");

for (var i = 0; i < btn.length; i++) {
  btn[i].addEventListener("click", myFunction);
}

function myFunction(event) {
  Array.forEach.call(0, btn, function(btn) {
    btn.classList.remove("clicked");
  });
  event.target.classList.add("clicked");
}
.clicked {
  color: pink;
}
<div>
  <button class="btn-color-black btn">Yo!</button>
  <button class="btn-color-black btn">Yo!</button>
  <button class="btn-color-black btn">Yo!</button>
</div>

All answers are good, but they don't remove the clicked class if the user clicks the same button again. To do it try this example:

let btn = document.getElementsByClassName("btn");

for (let i = 0; i < btn.length; i++) {
    btn[i].addEventListener("click", myFunction);
}

function myFunction() {
    if (!this.classList.contains("clicked")) {
        let prev = document.querySelector('.clicked')
        if (prev) prev.classList.remove("clicked");
        this.classList.add("clicked");
    } else {
        this.classList.remove("clicked");
    }
}
.clicked {
  color: pink;
}
<div>
  <button class="btn-color-black btn">Yo!</button>
  <button class="btn-color-black btn">Yo!</button>
  <button class="btn-color-black btn">Yo!</button>
</div>

This is the shortest version

Without for-loop nor forEach function, just Javascript function querySelector

function myFunction() {
  var current = document.querySelector('.clicked')
  if (current) current.classList.remove('clicked');      
  this.classList.add('clicked');
}

var btn = document.getElementsByClassName("btn");

for (var i = 0; i < btn.length; i++) {
  btn[i].addEventListener("click", myFunction);
}

function myFunction() {
  var current = document.querySelector('.clicked')
  if (current) current.classList.remove('clicked');      
  this.classList.add('clicked');
}
.clicked {
  color: pink;
}
<div>
  <button class="btn-color-black btn">Yo!</button>
  <button class="btn-color-black btn">Yo!</button>
  <button class="btn-color-black btn">Yo!</button>
</div>

发布评论

评论列表(0)

  1. 暂无评论