.slice()

.slice( start, [ end ] ) 官网原E文: jQuery

描述: 减少匹配元素集合由索引范围到指定的一个子集。

  • version added: 1.1.4.slice( start, [ end ] )

    start一个整数,指示0的位置上的元素开始被选中。如果为负,则表示从集合的末尾的偏移量。

    end一个整数,指示0的位置上被选中的元素停止。如果为负,则表示从集的末尾的偏移量。如果省略,范围,持续到集合的末尾。

如果提供的jQuery代表了一组DOM元素,.slice()方法从匹配元素的子集中构造一个新的jQuery对象。所提供的start索引标识的设置一个集合中的元素的位置;如果end被省略,这个元素之后的所有元素将包含在结果中。

考虑一个页上有一个简单的列表:

<ul>
  <li>list item 1</li>
  <li>list item 2</li>
  <li>list item 3</li>
  <li>list item 4</li>
  <li>list item 5</li>
</ul>

我们可以应用此方法到列表集合:

$('li').slice(2).css('background-color', 'red');

该调用的结果是一个红色背景添加大片3,4和5项。请注意,提供的索引是从零开始的,并指在jQuery对象元素的位置,不属于DOM树。

最终的参数允许我们选择的范围,以限制进一步。 例如:

$('li').slice(2, 4).css('background-color', 'red');

现在只有3和4项被选中。该索引数再次从零开始的;延伸的范围,但不包括指定的索引。

负指数

jQuery的.slice()方法是仿照的JavaScript 数组的.slice()方法。模仿的特点之一,它是有能力将负数传递startend的参数。如果提供一个负数,这表明立场从集的结尾开始,而不是开头。例如:

$('li').slice(-2, -1).css('background-color', 'red');

这一次只有项4是变成了红色,因为它是唯一的项目的两个从末端( -2 )和一个从端( -1 )范围。

Examples:

Example: Turns divs yellow based on a random slice.

<!DOCTYPE html>
<html>
<head>
  <style>
  div { width:40px; height:40px; margin:10px; float:left;
        border:2px solid blue; }
  span { color:red; font-weight:bold; }
  button { margin:5px; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
  <p><button>Turn slice yellow</button>
  <span>Click the button!</span></p>
  <div></div>
  <div></div>

  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>

  <div></div>
  <div></div>
<script>

    function colorEm() {
      var $div = $("div");
      var start = Math.floor(Math.random() *
                             $div.length);
      var end = Math.floor(Math.random() *
                           ($div.length - start)) +
                           start + 1;
      if (end == $div.length) end = undefined;
      $div.css("background", "");
      if (end) 
        $div.slice(start, end).css("background", "yellow");   
       else
        $div.slice(start).css("background", "yellow");
      
      $("span").text('$("div").slice(' + start +
                     (end ? ', ' + end : '') +
                     ').css("background", "yellow");');
    }

    $("button").click(colorEm);

</script>

</body>
</html>

Demo:

Example: Selects all paragraphs, then slices the selection to include only the first element.

$("p").slice(0, 1).wrapInner("<b></b>");

Example: Selects all paragraphs, then slices the selection to include only the first and second element.

$("p").slice(0, 2).wrapInner("<b></b>");

Example: Selects all paragraphs, then slices the selection to include only the second element.

$("p").slice(1, 2).wrapInner("<b></b>");

Example: Selects all paragraphs, then slices the selection to include only the second and third element.

$("p").slice(1).wrapInner("<b></b>");

Example: Selects all paragraphs, then slices the selection to include only the third element.

$("p").slice(-1).wrapInner("<b></b>");
jQuery 1.6 API 中文版Clove整理、修订