英文:
how do i change the color one by one to the items there with each click so that the previous one turns green again?
问题
<button>点击!</button>
<ul>
<li class="green">主页</li>
<li class="green">常见问题</li>
<li class="green">下拉菜单</li>
<li class="green">关于</li>
<li class="green">联系我们</li>
</ul>
let li = document.querySelectorAll('li');
let btn = document.querySelector('button');
for (let i = 0; i < li.length; i++) {
let number = 0;
btn.addEventListener('click', () => {
li[number++].classList.add('red');
if (number === li.length) {
number = 0;
}
})
}
英文:
<button>Click!</button>
<ul>
<li class="green">Home</li>
<li class="green">faq</li>
<li class="green">dropdown</li>
<li class="green">about</li>
<li class="green">contact</li>
</ul>
let li = document.querySelectorAll('li');
let btn = document.querySelector('button');
for(let i=0; i<li.length; i++) {
let number = 0;
btn.addEventListener('click', ()=>{
li[number++].classList.add('red')
if(number === li.length) {
number = 0
}
})
}
I wanted a single item to turn red with each click and the previous one to turn green again
答案1
得分: 1
你可以这样做,如果这对你有帮助。一旦你按下按钮,首先从每个元素中移除红色类,然后像你以前做的那样添加它。
let li = document.querySelectorAll('li');
let btn = document.querySelector('button');
for(let i=0; i<li.length; i++) {
let number = 0;
btn.addEventListener('click', () => {
for(let i=0; i<li.length; i++) {
li[i].classList.remove('red');
}
li[number++].classList.add('red');
if(number === li.length) {
number = 0;
}
});
}
.green {
color:green;
}
.red {
color:red;
}
<button>Click!</button>
<ul>
<li class="green">Home</li>
<li class="green">faq</li>
<li class="green">dropdown</li>
<li class="green">about</li>
<li class="green">contact</li>
</ul>
英文:
You can do it like this if this helps you. As soon as you press the button. Remove the red class from every element first and then add it like you were doing
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let li = document.querySelectorAll('li');
let btn = document.querySelector('button');
for(let i=0; i<li.length; i++) {
let number = 0;
btn.addEventListener('click', ()=>{
for(let i=0; i<li.length; i++) {
li[i].classList.remove('red')
}
li[number++].classList.add('red')
if(number === li.length) {
number = 0
}
})
}
<!-- language: lang-css -->
.green {
color:green;
}
.red {
color:red;
}
<!-- language: lang-html -->
<button>Click!</button>
<ul>
<li class="green">Home</li>
<li class="green">faq</li>
<li class="green">dropdown</li>
<li class="green">about</li>
<li class="green">contact</li>
</ul>
<!-- end snippet -->
答案2
得分: 0
使用JavaScript和一个事件监听器来实现这个功能:
document.querySelector(".menu").addEventListener("click", function (e) {
// 查找被点击的li元素
const clickedLi = e.target.closest("li");
if (!clickedLi) return;
// 检查是否已经有元素被点击
const selected = e.currentTarget.querySelector(".red");
if (selected) selected.classList.remove('red');
// 更新被点击元素的类
clickedLi.classList.add('red');
});
使用HTML和CSS来实现选择功能:
<ul class="menu">
<li><input type="radio" name="list" id="li1"><label for="li1">Foo</label></li>
<li><input type="radio" name="list" id="li2"><label for="li2">Bar</label></li>
<li><input type="radio" name="list" id="li3"><label for="li3">Baz</label></li>
<li><input type="radio" name="list" id="li4"><label for="li4">Cheese</label></li>
</ul>
英文:
Doing this with JavaScript and one event listener
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
document.querySelector(".menu").addEventListener("click", function (e) {
// find the li that was clicked
const clickedLi = e.target.closest("li");
if (!clickedLi) return;
// see if we had something clicked already
const selected = e.currentTarget.querySelector(".red");
if (selected) selected.classList.remove('red');
// update the class on what was clicked
clickedLi.classList.add('red');
});
<!-- language: lang-css -->
.green {
background-color: green;
}
li.red {
background-color: red;
}
<!-- language: lang-html -->
<ul class="menu">
<li class="green">Home</li>
<li class="green">faq</li>
<li class="green">dropdown</li>
<li class="green">about</li>
<li class="green">contact</li>
</ul>
<!-- end snippet -->
Using just html and css to make the selections
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.menu input[type="radio"] {
display: none;
}
.menu input[type="radio"]+label {
display: inline-block;
width: 100%;
background-color: green;
}
.menu input[type="radio"]:checked + label {
background-color: red;
}
<!-- language: lang-html -->
<ul class="menu">
<li><input type="radio" name="list" id="li1"><label for="li1">Foo</label></li>
<li><input type="radio" name="list" id="li2"><label for="li2">Bar</label></li>
<li><input type="radio" name="list" id="li3"><label for="li3">Baz</label></li>
<li><input type="radio" name="list" id="li4"><label for="li4">Cheese</label></li>
</ul>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论