.active
的类名table tr th span.sorting
。.active
添加到当前点击的目标元素的子项。<table>
<tr>
<th>
<span class="sorting"></span>
</th>
<th>
<span class="sorting"></span>
</th>
<th>
<span class="sorting active"></span>
</th>
</tr>
</table>
trigger.target.children[0].classList.add('active')
每当我点击active
元素并将<span class="sorting">
类分配给当前点击的块时,从<th>
删除所有active
类的最佳方法是什么?
请不要jQuery。
答案 0 :(得分:2)
您可以使用querySelectorAll
获取元素,使用Array.from
将输出转换为数组。
Array.from( document.querySelectorAll( "th .sorting" ) );
现在迭代它们并使用classList API
删除该类Array.from( document.querySelectorAll( "th .sorting" ) ).forEach( function( ele ){
ele.classList.remove( "active" );
});
最后到当前元素,您可以将该类添加到事件的currentTarget
event.currentTarget.classList.add( "active" );
<强>演示强>
var elements = Array.from( document.querySelectorAll( "th .sorting" ));
elements.forEach( function(ele){
ele.addEventListener( "click", function(){
elements.forEach( function( ele ){
ele.classList.remove( "active" );
});
ele.classList.add( "active" );
});
});
.active
{
background-color: #f00;
}
<table>
<tr>
<th>
<span class="sorting">A</span>
</th>
<th>
<span class="sorting">B</span>
</th>
<th>
<span class="sorting active">C</span>
</th>
</tr>
</table>
答案 1 :(得分:1)
在点击.sorting
元素时移除所有活动的类,然后在currentTarget上添加
let elements = Array.from(document.getElementsByClassName('sorting'));
elements.forEach(e => {
e.addEventListener('click', s => {
elements.forEach(el => el.classList.remove('active'));
s.currentTarget.classList.add('active');
});
})
<table>
<tr>
<th>
<span class="sorting"></span>
</th>
<th>
<span class="sorting"></span>
</th>
<th>
<span class="sorting active"></span>
</th>
</tr>
</table>
答案 2 :(得分:1)
var allTh = document.querySelectorAll("#tbl thead tr th");
for(var i = 0; i < allTh.length; i++){
allTh[i].addEventListener("click",function(e){
removeAllActiveClasses();
e.target.classList.add("active");
});
}
function removeAllActiveClasses(){
var allActiveClassElement = document.querySelectorAll(".active");
for(var i = 0; i < allActiveClassElement.length; i++){
allActiveClassElement[i].classList.remove("active");
}
}
.active{
color : blue;
}
<table id="tbl">
<thead>
<tr>
<th>
<span class="sorting">Col 1</span>
</th>
<th>
<span class="sorting">Col 2</span>
</th>
<th>
<span class="sorting active">Col 3</span>
</th>
</tr>
</thead>
</table>
答案 3 :(得分:1)
document.querySelectorAll(".active.sorting").forEach(e=>e.classList.remove("active"))
event.currentTarget.classList.add(".active")
我们对querySelector返回的值有forEach函数,所以我们可以遍历那个domArray,然后第二个代码行放在事件监听器的上下文中
答案 4 :(得分:1)
直接在HTMLCollection上工作也应该是一种可行的方法:
var elements = document.getElementsByClassName('sorting');
for(i=0;i<elements.length;i++) {
elements[i].className -= ' active';
}
答案 5 :(得分:0)
您可以尝试以下代码
var elems = Array.from(document.getElementsByClassName("sorting"));
//console.log(elems)
elems.forEach(e => e.addEventListener('click', function(event) {
elems.forEach(e => e.classList.remove("active"))
event.currentTarget.classList.add("active");
}));
&#13;
body {
font: 13px verdana
}
span.active {
color: red;
}
span {
display: block;
margin: 0 10px;
cursor: pointer
}
&#13;
<table>
<tr>
<th>
<span class="sorting">One</span>
</th>
<th>
<span class="sorting">Two</span>
</th>
<th>
<span class="sorting active">Three</span>
</th>
</tr>
</table>
&#13;