JSLite - Object access
If you have any questions, you are welcome to communicate in these places, and you are welcome to join the JSLite.io organization team for joint development!
each
Traverse a
JSLite
collection object and execute a function for each matching element. The this keyword points to the current item (passed as the second parameter of the function). If the function returns false, the traversal ends.
$("img").each(function(i){ this.src = "test" + i + ".jpg"; }); //⇒ 找到所有的img对象给设置src //⇒ 返回 [ <img src="test0.jpg" />, <img src="test1.jpg" /> ]
map
Traverse all node objects in the node object collection and return a new collection object
$(".box").map(function(index,item){ return $(this).text() }) //⇒ 返回 ["12boxOne", "6", "11", "22123456", "7123123"]
forEach
Similar to each, forEach traversal will not stop.
//遍历数组 [1,5,2,3].forEach(function(item,index,array){ console.log(item,index,array) }) //遍历节点 $("img").forEach(function(item,index,array){ console.log(item,index,array) })
eq
Specifies which element of the index the set of matching elements is. An integer indicating the position of the element, in base
0
. eq(index) ⇒ collection eq(-index) ⇒ collection
$("div").eq(0)//⇒ 返回数组第一个节点数组 [div#box.boxOne.box2.box3, init: function…] $("div").eq(-1)//⇒ 倒数第一个节点数组 $("div").eq(-2)//⇒ 倒数第二个节点数组
first
Get the first element in the current object collection. first() ⇒ collection
$('form').first()
get
Get all node objects or a single node object in the current object collection.
$("div").get(0)//⇒ 返回节点 <div id="box" class="boxOne box2 box3" ></div>
index
Get the position of an element. When the elemen parameter is not given, returns the position of the current element in the sibling node. .index() //The position of the first element in the object relative to its sibling elements .index(selector)
.index(element)
$("#box").index()//⇒ 4 $("div").index("#box")//⇒ 2 $("div").index($("#box"))//⇒ 2 $("div").index($("#box")[0])//⇒ 2
indexOf
Get the position of an element in this array in the currently acquired node array.
$("div").indexOf($("#box")[0]) //⇒ 2
length
The number of elements in the object.
$("img").length;//⇒ 2