Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript中条件语句中的For循环_Javascript_Jquery_For Loop_Foreach - Fatal编程技术网

Javascript中条件语句中的For循环

Javascript中条件语句中的For循环,javascript,jquery,for-loop,foreach,Javascript,Jquery,For Loop,Foreach,如果语句顺序为: if($(selected).children('li').children('ul')){ selected = $(selected).children('li').children('ul'); $(selected).attr('data-level','3'); if( $(selected).children('li').children('ul') ){ selected = $(selected).children('li').child

如果语句顺序为:

if($(selected).children('li').children('ul')){
  selected = $(selected).children('li').children('ul');
  $(selected).attr('data-level','3');

  if( $(selected).children('li').children('ul')  ){
    selected = $(selected).children('li').children('ul');
    $(selected).attr('data-level','4');

    if( $(selected).children('li').children('ul')  ){
      selected = $(selected).children('li').children('ul');
      $(selected).attr('data-level','5');

      if( $(selected).children('li').children('ul')  ){
        selected = $(selected).children('li').children('ul');
        $(selected).attr('data-level','6');

        if( $(selected).children('li').children('ul')  ){
          selected = $(selected).children('li').children('ul');
          $(selected).attr('data-level','7');
        }
      }
    }
  }
}

我只是想知道如何在一个简单的for循环中处理这个问题。谢谢你的帮助。

我想你想要这样的东西:

$'ul'.attr'data-level',函数{ 返回$this.parents'ul'。长度; }; [数据级别=0]{ 颜色:蓝色; } [数据级别=1]{ 颜色:绿色; } [数据级别=2]{ 颜色:红色; } 1. 2. 3.
所有条件都将始终传入该代码段。您需要检查返回的jQuery对象的长度。您的代码没有意义。首先选择的是从哪里开始的?为什么您可以将所有数据级别更改分组到一个if语句下?啊,唐布里奇的困惑。给下层选民注意:事实上,代码对你来说没有任何意义,并不意味着你完全理解这个问题。否决票不应该那么容易,谢谢你的回答。