当我点击其中一个li时,实现给当前的li添加一个新class属性,当再次点击当前li时移除class属性,实现类似于开关一样的效果?<ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li></ul>
5 回答
已采纳
Y_du
TA贡献43条经验 获得超5个赞
window.onload=function(){ var lis=document.getElementsByTagName('li'); for (var i = 0;i < lis.length; i++) { lis[i].index = i; lis[i].onclick=function(){ for(var count = 0; count < this.parentNode.children.length; count++){ if(count != this.index) { this.parentNode.children[count].classList.remove("on"); } else { if(this.classList == 0){ this.classList.add("on") } else { this.classList.remove("on") } } } } } };
_Jason
TA贡献1条经验 获得超0个赞
window.onload = function() { var openHidden = document.getElementsByClassName('open-hidden'); var detailsItems = document.getElementsByClassName('details-items'); var index = null; for (var i = 0; i < openHidden.length; i++) { openHidden[i].index = i; openHidden[i].onclick = function(){ index = this.index; toggleClass(index); }; }; function toggleClass(index){ if (detailsItems[index].className=="details-items open-box") { openHidden[index].childNodes[0].className = ""; detailsItems[index].className = "details-items"; } else{ openHidden[index].childNodes[0].className = "open-style"; detailsItems[index].className = "details-items open-box"; }; } };
Y_du
TA贡献43条经验 获得超5个赞
window.onload=function(){ var lis=document.getElementsByTagName('li'); for (var i = 0;i < lis.length; i++) { lis[i].index = i; lis[i].onclick=function(){ for(var count = 0; count < this.parentNode.children.length; count++){ if(count != this.index) { this.parentNode.children[count].classList.remove("on"); } else { if(this.classList == 0){ this.classList.add("on") } else { this.classList.remove("on") }e } } } } };
添加回答
举报
0/150
提交
取消