jQuery的遍历方式

JS的遍历

for(初始化值;循环条件;步长)

jQuery的遍历 

1、jq对象.each(callback)

2、$.each(obj,callback)

3、for……of:jq 3.0版本以后 提供的新方式

案例

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="../../js/jquery-3.3.1.min.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
        // jQuery的遍历
        $(function () {
            var citys = $("#city li");
            //jq方式一:
            citys.each(function (index,element) {
                alert(index+""+element.innerHTML)
            })

            //jq方式二:
            $.each(citys,function (index,element) {
                alert(index+""+element.innerHTML)
            })

            //jq方式三:
            for (element of citys){
                alert(element.innerHTML);
            }
        })

    </script>
</head>
<body>
<ul id="city">
    <li>北京</li>
    <li>上海</li>
    <li>天津</li>
    <li>重庆</li>
</ul>
</body>
</html>

 

 

猜你喜欢

转载自www.cnblogs.com/wzhsc/p/10415022.html