Jquery 每个列表项的增量

Jquery 每个列表项的增量,jquery,html,Jquery,Html,我试图增加每个列表项的z-index 这就是我迄今为止所尝试的: var photos = $('ul'); photos.each(function () { var photos = $(this).children(); var photosLen = photos.length; if (photosLen > 1) { photos.parent().addClass('album'); v

我试图增加每个列表项的
z-index

这就是我迄今为止所尝试的:

    var photos = $('ul');
  photos.each(function () {

      var photos = $(this).children();
      var photosLen = photos.length;
      if (photosLen > 1) {
          photos.parent().addClass('album');
          var i = 0;
          while (i < photosLen) {

              $(this).children().css({
                  'z-index': i
              });
          }
      }

  });

因为您已经在使用jQuery,所以代码可以简单地

  if (photosLen > 1) {
      photos.each(function(i) {
          $(this).css('zIndex', i);
      })
      .parent()
      .addClass('album');
  }

请注意
each()
z-index
属性的camelCase语法,因为您已经在使用jQuery,所以代码可以简单地

  if (photosLen > 1) {
      photos.each(function(i) {
          $(this).css('zIndex', i);
      })
      .parent()
      .addClass('album');
  }

注意
each()
z-index
属性的camelCase语法您忘记了在代码中增加
i
的值:

      while (i < photosLen) {

          $(this).children().css({
              'z-index': i
          });
          i++; 
      }
while(i
您忘记在代码中增加
i
的值:

      while (i < photosLen) {

          $(this).children().css({
              'z-index': i
          });
          i++; 
      }
while(i
不是
photos.parent()
$(这个)
?哈哈,不是。。。我需要更仔细地命名变量……真的吗?您不认为
$(this).children().parent()
$(this)
?:)相同是不是
photos.parent()
$(这个)
一样?哈哈,不是。。。我需要更仔细地命名变量……真的吗?您不认为
$(this).children().parent()
$(this)
?:)相同