[英]Remove same class from several element in javascript
I have 4 Button with class of active
and now I wanna remove all of those class and then only add 1 class to the one I clicked on. 我有4个按钮,其中class是active
,现在我想删除所有这些class,然后仅将1 class添加到我单击的那个class中。
<button class="active btn">btn1</button>
<button class="active btn">btn2</button>
<button class="active btn">btn3</button>
<button class="active btn">btn4</button>
here is code I wrote so far: 这是我到目前为止编写的代码:
let buttonOptions = document.querySelectorAll(".btn");
for (let i=0; i< buttonOptions.length; i++) {
buttonOptions.addEventListener('click', function() {
buttonOptions[i].classList.add("active");
});
}
but I don't know how should I remove those active
class. 但我不知道该如何删除这些active
班级。 and also I wanna know there is a way to avoid using for
specifically for removing class? 而且我想知道有一种方法,以避免使用for
专门用于去除类?
You could avoid using a for
loop, but you'd still have to iterate, so there's really no point. 您可以避免使用for
循环,但是您仍然必须进行迭代,因此实际上没有任何意义。 I'll use forEach
as it's supported on querySelector
and looks a little cleaner, but a for
loop is fine as well 我将使用forEach
因为它在querySelector
上受支持,并且看起来更干净一些,但是for
循环也很好
Same goes for binding the event handler, you have to iterate and target each element 绑定事件处理程序同样如此,您必须迭代并定位每个元素
let buttonOptions = document.querySelectorAll(".btn"); buttonOptions.forEach( el => el.addEventListener('click', function() { buttonOptions.forEach( els => els.classList.remove('active') ) this.classList.add("active"); }) )
.active {color : red}
<button class="active btn">btn1</button> <button class="btn">btn2</button> <button class="btn">btn3</button> <button class="btn">btn4</button>
var buttons = document.getElementsByClassName("btn"); function removeAllActive() { [].forEach.call(buttons, function(el) { el.classList.remove("active"); }); } removeAllActive(); [].forEach.call(buttons, function(el) { el.addEventListener("click", function() { removeAllActive(); el.classList.add("active"); }); });
.active { background-color: red; }
<button class="active btn">btn1</button> <button class="active btn">btn2</button> <button class="active btn">btn3</button> <button class="active btn">btn4</button>
document.getElementById('buttons').addEventListener('click', function(evt) { if (evt.target.classList.contains('btn')) { Array.from(document.querySelectorAll('.btn', this)).forEach(x => { x.classList.toggle('active', x == evt.target) }); } });
.active { color: red; }
<div id="buttons"> <button class="active btn">btn1</button> <button class="btn">btn2</button> <button class="btn">btn3</button> <button class="btn">btn4</button> </div>
Attaching a listener to each button is wasteful. 将侦听器附加到每个按钮上很浪费。 I attach a listener to a containing element, then wait for the event to bubble up to it. 我将侦听器附加到一个包含元素,然后等待事件冒泡。 If the click originated from one of the btn
-classed elements, then I find all the btn
-classed elements within it and switch their active
class based on whether they are the clicked button or not. 如果单击来自btn
分类的元素之一,那么我会在其中找到所有btn
分类的元素,并根据是否是单击按钮来切换其active
类。
Here is the easiest way to resolve the mentioned issue, 这是解决上述问题的最简单方法,
$(function){
$(document).on("click",".btn",function(){
$(".btn").removeClass("active");
$(this).addClass("active");
});
});
If you are allowed to use jquery, it will be much easier for you. 如果您被允许使用jquery,它将对您来说更加容易。 Please have a look on the below code,it may help you 请看下面的代码,它可能对您有帮助
<script> $( document ).ready(function() { $('.btn').removeClass('active'); $('.btn').click(function(e) { $('.btn').removeClass('active'); $(this).addClass('active'); }); }); </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <button class="active btn">btn1</button> <button class="active btn">btn2</button> <button class="active btn">btn3</button> <button class="active btn">btn4</button>
Try this one: 试试这个:
$(document).on("click",".btn",function(){
$(".btn").removeClass("active");
$(this).addClass("active");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.