简体   繁体   English

更改按钮的背景颜色 - 单击时更改,单击另一个按钮时删除

[英]Changing Background Colour of Button - Change when clicked, remove when another button is clicked

First time poster - have tried to find solution elsewhere but not able to.第一次发帖 - 试图在其他地方找到解决方案,但无法找到。

I have created a grid of buttons.我创建了一个按钮网格。 Upon clicking button 1, I want the background to change to a preset colour.单击按钮 1 后,我希望背景更改为预设颜色。 When another button is clicked (button 2), I would like the button(1) background to change back to the original, followed by the newly clicked button (button 2) to take on the preset colour.当单击另一个按钮(按钮 2)时,我希望按钮(1)背景变回原来的,然后是新单击的按钮(按钮 2)以呈现预设颜色。

Here is the HTML written so far:这是到目前为止编写的 HTML:

    <div class="tip-select">
      <p class="ttext">Select Tip %</p>
      <div class="tip">
        <div class="five-tip btn tips">5%</div>
        <div class="ten-tip btn tips">10%</div>
        <div class="fifteen-tip btn active-tip tips">15%</div>
        <div class="twentyfive-tip btn tips">25%</div>
        <div class="fifty-tip btn tips">50%</div>
        <div class="custom btn tips">Custom</div>
      </div>
    </div>

Here is the Javascript written so far:这是到目前为止编写的 Javascript:

tips.forEach(function (mov) {
  mov.addEventListener("click", handleClick);
});


function handleClick(event) {
tips.forEach(function (val) {
  if (event.target.innerHTMl == val.innerHTMl) {
    val.classList.add("active-tip");
    console.log(val);
  }
  });
}

Here is the current design for reference:这是当前的设计供参考:

改变网格

My thought is to iterate through the nodelist and assign active-tip as a class based on the button selected.我的想法是遍历节点列表并根据所选按钮将活动提示分配为 class。 At the moment though all of the buttons are changing colour when clicked.目前,尽管所有按钮在单击时都会改变颜色。

First, JavaScript is case sensitive, so innerHTMl should read innerHTML .首先, JavaScript 区分大小写,因此innerHTMl应为innerHTML But that isn't a sound method to achieve your wanted effect.但这不是实现您想要的效果的合理方法。

Also, tips needs to be an array, so use querySelectorAll to return a live collection ('live' means that only references to the elements are stored and not the elements).此外, tips需要是一个数组,因此使用querySelectorAll来返回一个实时集合(“实时”意味着只存储对元素的引用而不是元素)。 To make this into an array, use Array.from() .要将其制成数组,请使用Array.from()

remove all the active-tip styles first, and then add the new one to the required element.首先remove所有active-tip styles,然后将新的添加到所需的元素中。

 tips=Array.from(document.querySelectorAll('.tips')); tips.forEach(function (mov) { mov.addEventListener("click", handleClick); }); function handleClick(e) { tips.forEach((t)=>t.classList.remove("active-tip")); e.target.classList.add("active-tip"); }
 .active-tip {color:red}
 <div class="tip-select"> <p class="ttext">Select Tip %</p> <div class="tip"> <div class="five-tip btn tips">5%</div> <div class="ten-tip btn tips">10%</div> <div class="fifteen-tip btn active-tip tips">15%</div> <div class="twentyfive-tip btn tips">25%</div> <div class="fifty-tip btn tips">50%</div> <div class="custom btn tips">Custom</div> </div> </div>

You you can add a prameter to function which is the id or class of your div so when your function called your function take the innertext of the div with you id you get from pramerter. You you can add a prameter to function which is the id or class of your div so when your function called your function take the innertext of the div with you id you get from pramerter.

I suggest you compare the node objects rather than their innerHtml .我建议您比较节点对象而不是它们的innerHtml In addition, you are missing the else case to reset all non selected nodes (remove the active-tip class).此外,您错过了重置所有非选定节点的else情况(删除active-tip类)。

Here's an example这是一个例子

let tips = Array.from(document.getElementsByClassName('tips'))

tips.forEach(function (mov) {
  mov.addEventListener("click", handleClick);
});

function handleClick(event) {
  tips.forEach(function (val) {
    if (val == event.target) {
      val.classList.add("active-tip");
    } else {
      val.classList.remove("active-tip");
    }
  });
}

and the complete codepen example here https://codepen.io/beezital/pen/OJxoxmK以及此处的完整代码示例https://codepen.io/beezital/pen/OJxoxmK

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

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