博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
slice()如何在jQuery中工作
阅读量:2536 次
发布时间:2019-05-11

本文共 2863 字,大约阅读时间需要 9 分钟。

Earlier we looked how to get an , but sometimes we want to get subset of the element indexes array. We can use slice() method for this.

之前,我们研究了如何获取的 ,但有时我们想要获取元素索引数组的子集。 我们可以为此使用slice()方法。

jQuery slice() (jQuery slice())

The slice() method reduces the matched set of elements to a subset specified by a range of indices. This method accepts two integer values, start index and end index. This method will return the elements between the start and end index. The subset of elements includes the element at start index but excludes element at end index.

slice()方法将匹配的元素集减少为由一系列索引指定的子集。 此方法接受两个整数值,开始索引和结束索引。 此方法将返回开始索引和结束索引之间的元素。 元素子集包括开始索引处的元素,但不包括结束索引处的元素。

Here is the general syntax for using jQuery slice()

这是使用jQuery slice()的一般语法

  • selector.slice(start, end)

    selector.slice( 开始,结束

start: This is a mandatory parameter which specifies the starting index at which the elements begin to be selected. The index numbers start at 0 . Using a negative number indicates an offset from the end of the matched set of elements.

start :这是一个必需参数,它指定开始选择元素的起始索引。 索引编号从0开始。 使用负数表示从匹配的元素集的末尾偏移。

end: This is an optional parameter which specifies the ending index at which the elements stop being selected. The range of selection continues until the end of the matched set if we omit the end index.

end :这是一个可选参数,用于指定停止选择元素的结束索引。 如果我们省略结束索引,选择范围将一直持续到匹配集的结束。

jQuery slice()示例 (jQuery slice() example)

Following example demonstrates the jQuery slice() usage.

以下示例演示了jQuery slice()的用法。

jQuery Traversing slice

jQuery slice() demo

My index is 0, also -6 from last
My index is 1, also -5 from last
My index is 2, also -4 from last
My index is 3, also -3 from last
My index is 4, also -2 from last
My index is 5, also -1 from last

in this example, we have used both positive and negative indices for the start and end parameters.

在此示例中,我们对开始结束参数都使用了正索引和负索引。

$( "div" ).slice( 0,2).addClass("highlight1");: This will select the div elements between the start index 0 and end index 2 and changes the color to green. The slice() method used in this line is same as slice(-6,-4);.

$( "div" ).slice( 0,2).addClass("highlight1"); :这将在开始索引0和结束索引2之间选择div元素,并将颜色更改为绿色。 该行中使用的slice()方法与slice(-6,-4);

$( "div" ).slice( -3,-1 ).addClass("highlight"); This will select the div elements between the start index -3 and end index -1 and changes the color to yellow. The slice() method used in this line is same as slice(3,5);.

$( "div" ).slice( -3,-1 ).addClass("highlight"); 这将在开始索引-3和结束索引-1之间选择div元素,并将颜色更改为黄色。 此行中使用的slice()方法与slice(3,5);相同slice(3,5);

The jQuery slice() method is very useful when you want to get a subset of elements from the matched set. That’s all for now, we will look into more jQuery methods in coming posts.

当您想从匹配的集合中获取元素的子集时,jQuery slice()方法非常有用。 到此为止,我们将在以后的文章中研究更多的jQuery方法。

翻译自:

转载地址:http://odlzd.baihongyu.com/

你可能感兴趣的文章
程序员三个境界
查看>>
从微信小程序开发者工具源码看实现原理(一)- - 小程序架构设计
查看>>
ASP.NET 上的 Async/Await 简介
查看>>
Effective C++学习笔记(1)
查看>>
element-ui 组件源码分析整理笔记目录
查看>>
GridEh排序
查看>>
[oc学习笔记]多态
查看>>
Tomcat connectionTimeout问题定位处理
查看>>
【PP系列】SAP 取消报工后修改日期
查看>>
ZooKeeper学习第四期---构建ZooKeeper应用(转)
查看>>
JNday4-am
查看>>
UI控件(复习一下)
查看>>
window下自己主动备份数据库成dmp格式的bat写法
查看>>
Memcache存储大数据的问题
查看>>
HDU 5050 Divided Land(进制转换)
查看>>
python进阶学习笔记(三)
查看>>
javascript语法之Date对象与小案例
查看>>
Day45 jquery表格操作、轮播图
查看>>
POJ 2079 Triangle 旋转卡壳求最大三角形
查看>>
【模板】树链剖分
查看>>