简体   繁体   English

在Jquery / javascript中理解控制台中的“切片”

[英]understanding “slice” in console in Jquery/javascript

i am deconstructing a carasoul plugin called Unslider.js , and i have a difficulty understanding a little peice of code , so here i am on stack overflow . 我正在解构一个名为Unslider.js的carasoul插件,并且我很难理解一些代码,因此在这里我处于堆栈溢出状态。

My difficulty is understanding the below line : 我的困难是理解以下内容:

target = li.eq(index);

lets analyse that , 让我们分析一下

target is a variable . 目标是一个变量。

li is an object , actually let me clarify what li is , previously in the code using the find() method in Jquery which returns a set of child elements so basically li is a set of <li> elements . li是一个对象,实际上让我弄清楚li是什么,之前在代码中使用Jquery中的find()方法返回了一组子元素,因此li基本上是一组<li>元素。

next , eq is often used to filter element further . 接下来, eq通常用于进一步过滤元素。

index is ofcourse the current element (i am not sure about this interpretation though) . 索引当然是当前元素(尽管我不确定这种解释)。

so basically i understand the below line : 所以基本上我理解以下内容:

        target = li.eq(index);

target is the current li element, am i right , i think so . 目标是当前的li元素,是的,我是这样认为的。

well lets move on to what my real difficulty is , when i console.log(target) , i get the following results : 好吧,让我继续我真正的困难是什么,当我console.log(target)时,我得到以下结果:

"taget is" unslider.js:285
Object { length: 0, prevObject: Object, context: <div.banner>, selector: ">ul >li.slice(3,4)" } unslider.js:286
"taget is" unslider.js:285
Object { 0: <li>, length: 1, prevObject: Object, context: <div.banner>, selector: ">ul >li.slice(1,2)" } unslider.js:286
"taget is" unslider.js:285
Object { 0: <li>, length: 1, prevObject: Object, context: <div.banner>, selector: ">ul >li.slice(2,3)" } unslider.js:286
"taget is" unslider.js:285
Object { length: 0, prevObject: Object, context: <div.banner>, selector: ">ul >li.slice(3,4)" } unslider.js:286
"taget is" unslider.js:285
Object { 0: <li>, length: 1, prevObject: Object, context: <div.banner>, selector: ">ul >li.slice(1,2)" } unslider.js:286
"taget is" unslider.js:285
Object { 0: <li>, length: 1, prevObject: Object, context: <div.banner>, selector: ">ul >li.slice(2,3)" } unslider.js:286
"taget is" unslider.js:285
Object { length: 0, prevObject: Object, context: <div.banner>, selector: ">ul >li.slice(3,4)" } unslider.js:286
"taget is" unslider.js:285
Object { 0: <li>, length: 1, prevObject: Object, context: <div.banner>, selector: ">ul >li.slice(1,2)" } unslider.js:286
"taget is" unslider.js:285
Object { 0: <li>, length: 1, prevObject: Object, context: <div.banner>, selector: ">ul >li.slice(2,3)" } unslider.js:286
"taget is" unslider.js:285
Object { length: 0, prevObject: Object, context: <div.banner>, selector: ">ul >li.slice(3,4)" } unslider.js:286
"taget is" unslider.js:285
Object { 0: <li>, length: 1, prevObject: Object, context: <div.banner>, selector: ">ul >li.slice(1,2)" }

now what does this line mean .. 现在这行是什么意思..

selector: ">ul >li.slice(3,4)", 

i mean the slice(3,4) part and that part keeps changing with different values !! 我的意思是slice(3,4)部分,该部分以不同的值不断变化! it would be great if somebody could come along and explain what that is . 如果有人可以来解释那是一件很棒的事。

Thank you. 谢谢。

Alexander. 亚历山大。

Slice is used to return a subset of an array. Slice用于返回数组的子集。 In this case the values are always separated by one, so it will return an array of a single element. 在这种情况下,值始终由1分隔,因此它将返回单个元素的数组。

var li = ['a','b','c','d'];
console.log(li.slice(2,3));
>  ['c']

So it's just a way to select one element from the list, but generally applicable to selecting a range of elements as well. 因此,这只是从列表中选择一个元素的一种方法,但通常也适用于选择一系列元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM