简体   繁体   English

如何检查ul中的每个li文本颜色?

[英]how can I check each li text color in ul?

I want to change the text color of <li> one by one when I click the change button. 单击更改按钮时,我想逐个更改<li>的文本颜色。 Once all the <li> text is red it'll show the alert message "all li text color changed!". 一旦所有<li>文本都是红色,它将显示警告消息“所有li文本颜色已更改!”。

My html code is: 我的HTML代码是:

<html>
  <body>
    <button id="change">change li text-color</button>
    <ul id="nav">
      <li>one</li>
      <li>two</li>
      <li>three</li>
      <li>four</li>
      <li>five</li>
      <li>six</li>
      <li>seven</li>
    </ul>
  </body>
</html>

and my script is (jquery v1.5.2): 我的脚本是(jquery v1.5.2):

$(document).ready(function ()
{
  $('#change').click(function ()
  {
    $('#nav li').each(function ()
    {
        $(this).css('color', '#F00');
    });
  });
});

I think you want change colors one by one with each click. 我想你想要每次点击一个接一个地改变颜色。 Add a counter: 添加一个柜台:

$(document).ready(function ()
{
  var len = $('#nav li').length,
      cnt = 0;
  $('#change').click(function ()
  {
    if (cnt < len)
    {
      $('#nav li:eq('+cnt+')').css('color', '#F00');
      cnt++;
    } else {
      alert("all li text color changed!");
    }
  });
});

demo: http://jsfiddle.net/SUtED/ 演示: http//jsfiddle.net/SUtED/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM