每个列表项的增量

时间:2012-06-20 08:24:47

标签: 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');
          var i = 0;
          while (i < photosLen) {

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

  });

我预计每个列表项都会从z-index: 1;转到z-index: 3;但是它没有这样做。只需将数组的长度添加到每个列表项。

HTML :(代码仅适用于第一个无序列表)

<ul>

    <li><img src="http://i.imgur.com/PuwwFs.jpg" alt=""></li>
    <li><img src="http://i.imgur.com/cjAGks.jpg" alt=""></li>
    <li><img src="http://i.imgur.com/zA4lCs.jpg" alt=""></li>

  </ul>
    <ul>

        <li><img src="http://i.imgur.com/PuwwFs.jpg" alt=""></li>


    </ul>

2 个答案:

答案 0 :(得分:5)

由于您已经在使用jQuery,因此代码可能只是

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

请注意each()z-index属性

的camelCase语法

答案 1 :(得分:0)

您忘记在代码中增加i的值:

      while (i < photosLen) {

          $(this).children().css({
              'z-index': i
          });
          i++; 
      }
相关问题