admin 管理员组文章数量: 1086019
I am working on a project that requires me to toggle the class of li element every time I click on it.
I have the following code:
var list = document.getElementById('nav').children[0];
for (var i = 0; i < list.children.length; i++) {
var el = list.children[i];
el.addEventListener("click", function() {
el.classList.toggle("myClass");
});
}
<div id='nav'>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
</div>
I am working on a project that requires me to toggle the class of li element every time I click on it.
I have the following code:
var list = document.getElementById('nav').children[0];
for (var i = 0; i < list.children.length; i++) {
var el = list.children[i];
el.addEventListener("click", function() {
el.classList.toggle("myClass");
});
}
<div id='nav'>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
</div>
But this code adds the class only to the last li element no matter which li I click on, I have taken reference from Use JavaScript to change the class of an <li> and tried to tailor the same to my needs, but there is something wrong here. Any help will be really appreciated.
Share Improve this question edited Jul 24, 2018 at 9:30 Zakaria Acharki 67.5k15 gold badges78 silver badges106 bronze badges asked Jul 24, 2018 at 9:17 CodeHumorCodeHumor 451 silver badge7 bronze badges4 Answers
Reset to default 4You shouldn't attach the click event inside the loop, create a function out of the loop and call it when you attach the click event like :
var list_items = document.querySelectorAll('#nav>ul>li');
for (var i = 0; i < list_items.length; i++) {
list_items[i].addEventListener("click", toggle);
}
function toggle() {
this.classList.toggle("myClass");
}
.myClass {
color: green;
}
<div id='nav'>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
</div>
Change el.classList.toggle("myClass");
to this.classList.toggle("myClass");
and it will work, as it refers to the current element
Change the function inside the loop into an IIFE to solve the closure issue that binds only the last element to the function.
var list = document.getElementById('nav').children[0];
for (var i = 0; i < list.children.length; i++) {
list.children[i].addEventListener("click", (function(j) {
return function() {
var el = list.children[j];
el.classList.toggle("myClass");
}
}(i)));
}
.myClass {
background-color: red;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='nav'>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
</div>
var li = document.querySelectorAll("li");
for(var i of li){
i.addEventListener("click",function(){
this.classList.toggle("myClass")
})
};
.myClass {
color: green;
}
<div id='nav'>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
</div>
本文标签: htmlToggle the class of unordered list items using simple JavascriptStack Overflow
版权声明:本文标题:html - Toggle the class of unordered list items using simple Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744092654a2532322.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论