js的遍历方式
for(初始化值;循环结束条件;步长)
jq的遍历方式
- jq对象.each(callback)
语法
jquery对象.each(function(index,element){})
index:就是元素在集合中的索引
element:就是集合中的每一个元素对象
this:集合中的每一个元素对象
回调函数返回值
true:如果当前function返回为false,则结束循环(break)。
false:如果当前function返回为true,则结束本次循环,继续下次循环(continue)
例子
citys.each(function (index,element) {
//li对象 第一种方式 this
alert(this.innerHTML);
alert($(this).html());
// 获取li对象 第二种方式 在回调函数中定义参数 index(索引) element(元素对象)
alert(index+":"+element.innerHTML);
alert(index+":"+$(element).html());
})
-
$.each(object, [callback])
-
for…of: jquery3.0 版本之后提供的方式
语法
for(元素对象 of 容器对象)
例子
for(li of citys){
alert($(li).html());
}