如何在每次单击时逐个更改项目的颜色,以便以前的项目再次变为绿色?

huangapple go评论59阅读模式
英文:

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;
        }
    })
}
英文:
&lt;button&gt;Click!&lt;/button&gt;
&lt;ul&gt;
    &lt;li class=&quot;green&quot;&gt;Home&lt;/li&gt;
    &lt;li class=&quot;green&quot;&gt;faq&lt;/li&gt;
    &lt;li class=&quot;green&quot;&gt;dropdown&lt;/li&gt;
    &lt;li class=&quot;green&quot;&gt;about&lt;/li&gt;
    &lt;li class=&quot;green&quot;&gt;contact&lt;/li&gt;
&lt;/ul&gt;
let li = document.querySelectorAll(&#39;li&#39;);

let btn = document.querySelector(&#39;button&#39;);

for(let i=0; i&lt;li.length; i++) {
    let number = 0;

    btn.addEventListener(&#39;click&#39;, ()=&gt;{
        li[number++].classList.add(&#39;red&#39;)
        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(&#39;li&#39;);

let btn = document.querySelector(&#39;button&#39;);

for(let i=0; i&lt;li.length; i++) {
    let number = 0;
    btn.addEventListener(&#39;click&#39;, ()=&gt;{
      for(let i=0; i&lt;li.length; i++) {
      	li[i].classList.remove(&#39;red&#39;)
      }
      li[number++].classList.add(&#39;red&#39;)
      if(number === li.length) {
         number = 0
      }
    })
}

<!-- language: lang-css -->

.green {
   color:green;
}

.red {
   color:red;
}

<!-- language: lang-html -->

&lt;button&gt;Click!&lt;/button&gt;
&lt;ul&gt;
    &lt;li class=&quot;green&quot;&gt;Home&lt;/li&gt;
    &lt;li class=&quot;green&quot;&gt;faq&lt;/li&gt;
    &lt;li class=&quot;green&quot;&gt;dropdown&lt;/li&gt;
    &lt;li class=&quot;green&quot;&gt;about&lt;/li&gt;
    &lt;li class=&quot;green&quot;&gt;contact&lt;/li&gt;
&lt;/ul&gt;

<!-- 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(&quot;.menu&quot;).addEventListener(&quot;click&quot;, function (e) {
  // find the li that was clicked
  const clickedLi = e.target.closest(&quot;li&quot;);
  if (!clickedLi) return;
  
  // see if we had something clicked already
  const selected = e.currentTarget.querySelector(&quot;.red&quot;);
  if (selected) selected.classList.remove(&#39;red&#39;);
  
  // update the class on what was clicked
  clickedLi.classList.add(&#39;red&#39;);
});

<!-- language: lang-css -->

.green {
  background-color: green;
}

li.red {
  background-color: red;
}

<!-- language: lang-html -->

&lt;ul class=&quot;menu&quot;&gt;
  &lt;li class=&quot;green&quot;&gt;Home&lt;/li&gt;
  &lt;li class=&quot;green&quot;&gt;faq&lt;/li&gt;
  &lt;li class=&quot;green&quot;&gt;dropdown&lt;/li&gt;
  &lt;li class=&quot;green&quot;&gt;about&lt;/li&gt;
  &lt;li class=&quot;green&quot;&gt;contact&lt;/li&gt;
&lt;/ul&gt;

<!-- 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=&quot;radio&quot;] {
  display: none;
}

.menu input[type=&quot;radio&quot;]+label {
  display: inline-block;
  width: 100%;
  background-color: green;
}

.menu input[type=&quot;radio&quot;]:checked + label {
  background-color: red;
}

<!-- language: lang-html -->

&lt;ul class=&quot;menu&quot;&gt;
  &lt;li&gt;&lt;input type=&quot;radio&quot; name=&quot;list&quot; id=&quot;li1&quot;&gt;&lt;label for=&quot;li1&quot;&gt;Foo&lt;/label&gt;&lt;/li&gt;
  &lt;li&gt;&lt;input type=&quot;radio&quot; name=&quot;list&quot; id=&quot;li2&quot;&gt;&lt;label for=&quot;li2&quot;&gt;Bar&lt;/label&gt;&lt;/li&gt;
  &lt;li&gt;&lt;input type=&quot;radio&quot; name=&quot;list&quot; id=&quot;li3&quot;&gt;&lt;label for=&quot;li3&quot;&gt;Baz&lt;/label&gt;&lt;/li&gt;
  &lt;li&gt;&lt;input type=&quot;radio&quot; name=&quot;list&quot; id=&quot;li4&quot;&gt;&lt;label for=&quot;li4&quot;&gt;Cheese&lt;/label&gt;&lt;/li&gt;
&lt;/ul&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月9日 01:20:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定