简体   繁体   English

使用 javascript 单击时更改按钮的颜色

[英]Change the color of a button when clicked with javascript

i want to change the color of a button ONLY when clicked, and when i click OTHER element i want it to change to its original color, I already get how to change the color, but i dont know what could be the logic to return it back to the original background when I click other button我只想在单击时更改按钮的颜色,当我单击其他元素时我希望它更改为原始颜色,我已经知道如何更改颜色,但我不知道返回它的逻辑是什么当我点击其他按钮时回到原来的背景

<div class="button-categories">
  <a href="#" class="button-categories-link">Candidates</a>
</div>
<div class="button-categories">
  <a href="#" class="button-categories-link">Contacts/Guests</a>
</div>
<div class="button-categories">
  <a href="#" class="button-categories-link">Jobs</a>
</div>
<div class="button-categories">
  <a href="#" class="button-categories-link">Clients</a>
</div>

<script>
  categoriesButton.forEach(function (button, index) {
    button.addEventListener('click', function (e) {
      this.style.background = '#1976d2';
      this.style.color = '#fff';
    });
  });
</script>

You'll need to set up an event handler so that when any button gets clicked, all the buttons are reset so that no one of them is active.您需要设置一个事件处理程序,以便在单击任何按钮时重置所有按钮,这样就没有一个按钮处于活动状态。 Then, set the clicked button to be active by styling it.然后,通过设置样式将单击的按钮设置为活动状态。 But, instead of setting up multiple handlers, just set up one at the document level that will receive any clicks via bubbling (" event delegation ").但是,不要设置多个处理程序,只需在document级别设置一个,它将通过冒泡(“事件委托”)接收任何点击。

 // Get all the buttons into a node list let buttons = document.querySelectorAll(".button-categories-link"); // Set an event handler on the document so that when // any element is clicked, the event will bubble up to it document.addEventListener("click", function(evt){ // Check to see if it was a button that was clicked if(evt.target.classList.contains("button-categories-link")){ // Loop over all the buttons & remove the active class buttons.forEach(function(button){ button.classList.remove("active"); }); // Make the clicked button have the active class evt.target.classList.add("active"); } });
 .active { background-color:#ff0; }
 <div class="button-categories"> <a href="#" class="button-categories-link">Candidates</a> </div> <div class="button-categories"> <a href="#" class="button-categories-link">Contacts/Guests</a> </div> <div class="button-categories"> <a href="#" class="button-categories-link">Jobs</a> </div> <div class="button-categories"> <a href="#" class="button-categories-link">Clients</a> </div>

But, you really shouldn't be using hyperlinks if you aren't using them for navigation.但是,如果您不使用超链接进行导航,那么您真的不应该使用超链接。 That's semantically incorrect and will cause problems for users who rely on assistive technologies for web page access.这在语义上是不正确的,并且会给依赖辅助技术访问 web 页面的用户带来问题。 Instead, any visible element can be made to be clickable by setting up a click event handler for it.相反,任何可见元素都可以通过为其设置click事件处理程序来使其可点击。 But, in your case, since you want "button-like" behavior, why not use the button element?但是,在您的情况下,既然您想要“类似按钮”的行为,为什么不使用button元素呢? You can style them however you like if you don't like the native presentation.如果您不喜欢本机演示,您可以根据自己的喜好设置它们的样式。

 // Get all the buttons into a node list let buttons = document.querySelectorAll(".button-categories"); // Set an event handler on the document so that when // any element is clicked, the event will bubble up to it document.addEventListener("click", function(evt){ // Check to see if it was a button that was clicked if(evt.target.classList.contains("button-categories")){ // Loop over all the buttons & remove the active class buttons.forEach(function(button){ button.classList.remove("active"); }); // Make the clicked button have the active class evt.target.classList.add("active"); } });
 button.button-categories { display:block; border:none; background-color:rgba(0,0,0,0); } button.button-categories.active { background-color:rgba(255,255,0,1); }
 <button class="button-categories">Candidates</button> <button class="button-categories">Contacts/Guests</button> <button class="button-categories">Jobs</button> <button class="button-categories">Clients</button>

blur won't do the trick; blur不会解决问题; you'd use that for elements that hold focus like input .您会将其用于像input这样保持焦点的元素。 Instead, when one of the buttons is clicked, you'll need to go through the other buttons in the collection and reset their colors before setting the color of the clicked button:相反,当单击其中一个按钮时,您需要通过集合中的其他按钮 go 并在设置单击按钮的颜色之前重置它们的 colors :

categoriesButton.forEach(button => {
  button.addEventListener('click', function (e) {
    // Reset all buttons:
    const buttons = document.getElementsByClassName('button-categories');
    Array.from(buttons).forEach(b => {
      b.style.background = 'transparent';
      b.style.color = 'auto';
    });  
    // Apply colors for this button:
    this.style.background = '#1976d2';
    this.style.color = '#fff';
  });
});

(The answer using .active is actually better if you have only these buttons. When I read the question, I assumed you'd want to keep one of your buttons colored even when another button, unrelated to this button set, is clicked.) (如果您只有这些按钮,使用.active的答案实际上更好。当我阅读这个问题时,我假设您想要保持其中一个按钮的颜色,即使单击与此按钮集无关的另一个按钮也是如此。)

You have to write onblur event to change the button color back to its original color.您必须编写 onblur 事件才能将按钮颜色更改回其原始颜色。 So, when you click on other element, onblur event will trigger and change the color.所以,当你点击其他元素时,onblur 事件将触发并改变颜色。

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

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