简体   繁体   English

更改所有单击按钮的颜色

[英]Change color of all clicked buttons

I have a set of buttons in an html page of the following form:我在以下形式的 html 页面中有一组按钮:

<button
      id="testID"
      mat-mini-fab
      ngClass="list-button"
      (click)="onClick($event)"
    >
      Press
    </button>

I try to change the color of each button belonging to.list-button class after clicking on it using the following css code:我尝试使用以下 css 代码更改属于 .list-button class 的每个按钮的颜色:

.list-button:focus {
  background-color: #7d698d;
}

However, while the color of the button I click each time changes (the color of all the previously clicked buttons also changes back to their original color).然而,虽然我每次点击的按钮颜色都会改变(所有之前点击的按钮的颜色也会变回原来的颜色)。 How could I fix it?我该如何解决? I want all the clicked buttons to remain their new color.我希望所有单击的按钮都保持其新颜色。

I also tried assigning an id to the buttons of this class and changing their color inside the onClick() method as follows without success.我还尝试为这个 class 的按钮分配一个 id,并在 onClick() 方法中更改它们的颜色,如下所示但没有成功。 The same problem remains.同样的问题仍然存在。 Could you help me, please?请问你能帮帮我吗?

  onclick(event: any) {
    const btn = document.getElementById('testID');
    if (btn) btn.style.backgroundColor = '#7d698d';
  }

No need for js.不需要js。 Use the focus selector in your stylesheet.在样式表中使用focus选择器。

The :focus selector is allowed on elements that accept keyboard events or other user inputs. :focus选择器允许在接受键盘事件或其他用户输入的元素上使用。

~ W3 ~ W3

 button.list-button:focus { background-color: #7d698d; color: white; }
 <button class="list-button">Click</button>

Edit ~ with js.编辑~用js。 This method uses .querySelectorAll("button") and toggles the styled class .focus each time the button(s) are clicked.此方法使用.querySelectorAll("button")并在每次单击按钮时切换样式 class .focus This works well when having multiple buttons.这在有多个按钮时效果很好。

 // Get all the elements on the basis of query selector (button) let btn = document.querySelectorAll("button"); for (var i = 0; i < btn.length; i++) { (function(index) { btn[index].addEventListener("click", function() { console.log("Clicked Button: " + index); let isPresent = false; // Checks if the class is present or not this.classList.forEach(function(e, i) { if (e == "focus") { isPresent = true; } else { isPresent = false; } }); // Toggles the presence of class (.focus) on the basis of the isPresent variable if (isPresent) { this.classList.remove("focus"); } else { this.classList.add("focus"); } }); })(i); }
 .focus { background-color: #7d698d; color: white; cursor: pointer; }
 <button id="btn">Click</button> <button id="btn">Click</button> <button id="btn">Click</button>

You need to get the id of the button that fired the event您需要获取触发事件的按钮的 ID

onclick(event: any) {
  const btn = document.getElementById(event.target.id);
  if (btn) btn.style.backgroundColor = '#7d698d';
}

The reason why the color of the previously clicked buttons is resetting after you click on another one is that the focus is set on the last clicked button.单击另一个按钮后先前单击的按钮的颜色会重置的原因是焦点设置在最后单击的按钮上。 Try using the code in the Venkatesh's answer.尝试使用 Venkatesh 答案中的代码。

  • ID must be unique ID 必须是唯一的
  • You can use the function and pass el which will refer to this as an argument function onClick(el){......} and use it onClick(this)您可以使用 function 并传递el ,它将引用this作为参数function onClick(el){......}并使用它onClick(this)
  • No need to get the id you can directly use el.style or event.target.style see the next examples无需获取id你可以直接使用el.styleevent.target.style看下一个例子

 function onClick(el){ el.style.backgroundColor = '#7d698d'; }
 <button id="testID-1" mat-mini-fab ngClass="list-button" onclick="onClick(this)" > Press </button> <button id="testID-2" mat-mini-fab ngClass="list-button" onclick="onClick(this)" > Press </button>

Also You can use the function and pass event which equal event as an argument function onClick(event){......} and use it onClick(event)可以使用 function 并传递等于eventevent作为参数function onClick(event){......}并使用它onClick(event)

 function onClick(event){ event.target.style.backgroundColor = '#7d698d'; }
 <button id="testID-1" mat-mini-fab ngClass="list-button" onclick="onClick(event)" > Press </button> <button id="testID-2" mat-mini-fab ngClass="list-button" onclick="onClick(event)" > Press </button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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