jquery--遍历

做~自己de王妃 提交于 2019-12-05 19:50:45

1、遍历元素下所有子元素  (不包括孙子级)

 

$("#taskPropertyForm").children().each(function (i, item) {
                var $this = $(item);
                var _this = item;
                console.log(i);
                console.log(item.tagName);
            });

 

2、遍历元素下所有的子元素(包括所有子级,孙子及。。。)

 

$("#taskPropertyForm").find("*").each(function (i, item) {
                var $this = $(item);
                var _this = item;
                console.log(i);
                console.log(item.tagName);
            });

 

3、遍历对象

 

var b = new Object;
            b.property = [{
                "id": "f_p_id",
                "text": "span",
                "value": "1111"
            }, {
                "id": "f_p_name",
                "text": "input",
                "value": "222222"
            }, {
                "id": "f_p_info",
                "text": "textarea",
                "value": "333333"
            }];

            $.each(b.property, function (i, item) {
                console.log(item);
            });
// b.property.forEach(function () {
            //     console.log($(this).id);
            // });
 

4、js遍历对象

var objn = {};

        objn.nodes = {};


        for (i in obj.nodes) {
            // console.log(obj.nodes[i]);
            var nodes = {};

            objn.nodes[obj.nodes[i].id] = {
                "name": obj.nodes[i].name,
                "id": obj.nodes[i].id
            }
        }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!