js 恶心的遍历

  1. 云栖社区>
  2. 博客>
  3. 正文

js 恶心的遍历

cometwo123 2016-04-02 16:21:00 浏览1577 评论0

摘要: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Document</title> <script type="text/javascript" src="js/jquery-2.

这里写图片描述

<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Document</title>
        <script type="text/javascript" src="js/jquery-2.1.4.js"></script>
        <script type="text/javascript">
            //          $.each(window, function(i, v) {
            //              document.writeln(i + ':' + v + '<br />')
            //          });
            //          for(i in window){
            //              document.writeln(i + ':----->' + window[i] + '<br />')
            //          }
            var json = [{
                "id": "1",
                "tagName": "apple"
            }, {
                "id": "2",
                "tagName": "orange"
            }, {
                "id": "3",
                "tagName": "banana"
            }, {
                "id": "4",
                "tagName": "watermelon"
            }, {
                "id": "5",
                "tagName": "pineapple"
            }];
            //第一种方法$.each
            document.writeln('<h3>第一种方法$.each</h3>');
            $.each(json, function(i, v) {
                document.writeln(i + ':->(' + v.id + "," + v.tagName + ')<br />');
            });
            //第二种方法for in
            document.writeln('<h3>第二种方法for in</h3>');
            for (i in json) {
                document.writeln(i + ':->' + json[i].id + ':' + json[i].tagName + '<br />')
            }
        </script>
    </head>

    <body>

    </body>

</html>
【云栖快讯】阿里巴巴小程序繁星计划,20亿补贴第一弹云应用立即开通购买,限量从速!  详情请点击

网友评论