JS的遍历方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/jquery-3.4.1.min.js"></script>
<script>
$(function () {
var citys = $("#city li");
for (var i = 0; i < citys.length; i++) {
alert(i+":"+citys[i].innerHTML);
}
</script>
</head>
<body>
<ul id="city">
<li>北京</li>
<li>成都</li>
<li>上海</li>
<li>天津</li>
</ul>
</body>
</html>
JQ的遍历方式
- jq对象.each(callback)
$(function () {
var citys = $("#city li");
citys.each(function (index,element) {
alert(this.innerHTML);
alert($(this).html());
alert(index+":"+element.innerHTML);
alert(index+":"+$(element).html());
})
- 全局遍历 $.each(object, [callback])
$(function () {
var citys = $("#city li");
$.each(citys,function () {
alert($(this).html())
})
})
- for…of:
$(function () {
var citys = $("#city li");
for (li of citys){
alert($(li).html())
}
})