[英]Change button opacity of buttons that are not clicked
Hi I've got a question in how I would change the opacity of a list of buttons where if a button is clicked, the opacity of the rest of the buttons would change to 0.5.嗨,我有一个问题,我将如何更改按钮列表的不透明度,如果单击按钮,则按钮的 rest 的不透明度将更改为 0.5。 And the one that was clicked would remain at 1.
并且被点击的那个将保持在 1。
So far I've got the below where I can change the opacity of the button that's clicked but struggling to achieve what I've said above.到目前为止,我已经获得了以下内容,我可以在其中更改单击的按钮的不透明度,但努力实现我上面所说的。 In jquery I know you can use.not(this) but not sure how to do this is JS.
在 jquery 我知道你可以使用.not(this) 但不知道如何做到这一点是 JS。
EDIT: In my buttons I also have a span tag which holds the copy.编辑:在我的按钮中,我还有一个保存副本的 span 标签。 When you click on the span tag this looks to cause issues with the opacity as well.
当您单击跨度标签时,这看起来也会导致不透明度问题。
Button would like this, my apologies for not adding this in at the start Button 想要这个,我很抱歉没有在一开始就添加这个
<button class="variantClass"> <span>Button</span> </button
var buttonSelector = document.querySelectorAll('button.variantClass'); buttonSelector.forEach(function (el){ el.addEventListener("click", function(){ this.style.opacity = "0.5"; }) });
.variantClass { background: black; color: white; padding: 30px;}
<button class="variantClass"> Button </button> <button class="variantClass"> Button </button> <button class="variantClass"> Button </button> <button class="variantClass"> Button </button> <button class="variantClass"> Button </button> <button class="variantClass"> Button </button>
var buttonSelector = document.querySelectorAll('button.variantClass'); buttonSelector.forEach(function (el){ el.addEventListener("click", function(ev){ buttonSelector.forEach(function(button) { button.style.opacity = '0.5' }) this.style.opacity = 1 }) });
.variantClass { background: black; color: white; padding: 30px;}
<button class="variantClass"> <span>Button</span> </button> <button class="variantClass"> <span>Button</span> </button> <button class="variantClass"> <span>Button</span> </button> <button class="variantClass"> <span>Button</span> </button> <button class="variantClass"> <span>Button</span> </button> <button class="variantClass"> <span>Button</span> </button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.