2

我正在返回一个对象,我希望它能够循环并通过类或 ID 将其内容绑定到 DOM 对象。

有没有推荐的方法来做到这一点而不必手动分配每个元素?

最佳案例场景是一个函数,它实际上将创建一个包含数据的元素。

...否则我会手动创建和分配所有数据,而且还有很多。

{
    "user_profile": {
        "user_meta_first_name": "asdasd",
        "user_meta_last_name": "asdasd",
        "user_meta_billing_first_name": "asdasd",
        "user_meta_billing_last_name": "asdasd",
        "user_meta_billing_address_1": "2589 asdasd Rd.",
        "user_meta_billing_address_2": "",
        "user_meta_billing_city": "asdsdasd",
        "user_meta_billing_postcode": "VVV 344",
        "user_meta_billing_country": "CA",
        "user_meta_billing_state": "AB",
        "user_meta_billing_email": "admin@thebandagency.ca",
        "user_meta_billing_phone": "2343423434",
        "user_meta_shipping_first_name": "asdasd",
        "user_meta_shipping_last_name": "asdasd",
        "user_meta_shipping_address_1": "2589 asdasd Rd.",
        "user_meta_shipping_address_2": "",
        "user_meta_shipping_city": "asdasd",
        "user_meta_shipping_postcode": "VVV 344",
        "user_meta_shipping_country": "CA",
        "user_meta_shipping_state": "AB",
        "user_meta_shipping_email": "",
        "user_meta_shipping_phone": "",
        "user_meta_paying_customer": "1"
    },
    "pet_profiles": {
        "2000": {
            "pet_name": "Wally the Wonder Pup",
            "pet_tag_serial": "V140000",
            "pet_tag_pin": "XGZSVEMZ",
            "pet_tag_expiry": "December 8, 2013",
            "pet_tag_active": "1",
            "pet_tag_size": "1",
            "pet_tag_design": "Basket Case"
        }
    }
}
4

2 回答 2

0

我假设您的对象是一个数组。您可以使用eachJQuery 函数循环遍历您的对象,然后在其中创建一个包含您的对象数据的元素。

$.each(map, function(key, value) { 
  $('#mainContainer').append('<div id="'+key+'">'+value+'</div>');
});
于 2012-12-26T23:00:33.553 回答
0

如果这只是 jsonData,而您想将其全部显示在屏幕上,则可以使用这种递归方法。我建议对其进行编辑,因为它非常简单。http://pastebin.com/EzTZHJxW

(function () {
     var jsonData = dataSentFromServer();
     var detailsElement = document.getElementById("Details");
     function newDiv(txt) {
         var createDiv = document.createElement("div");
         if (txt != undefined)
             createDiv.innerHTML = txt;
         return createDiv;
     }

     var depth = 1;
     (function ComposeGraph(obj, el) {
         var ElementArray = [];
         var ChildArray = [];
         $.each(obj, function (name, value) {
             if (!$.isArray(value)) {

                 var appender = newDiv(name + " __:__ " + value);
                 appender.setAttribute("style", "margin-left:" + 10 * depth + "px;");
                 if ($.isPlainObject(value)) {
                     appender.innerHTML = "<b>" + appender.innerHTML + "</b>";
                     depth++;
                     ComposeGraph(value, appender);
                     depth--;
                     ElementArray.push(el);
                     ChildArray.push(appender);
                 } else {
                     el.appendChild(appender);
                 }
             } else {
                 var appender = newDiv(name + " __:__ " + value);
                 appender.setAttribute("style", "margin-left:" + 10 * depth + "px;");
                 appender.innerHTML = "<b>" + appender.innerHTML + "</b>";
                 for (var i = 0, len = value.length; i < len; i++) {
                     depth++;
                     ComposeGraph(value[i], appender);
                     depth--;
                 }
                 ElementArray.push(el);
                 ChildArray.push(appender);
             }
         });
         for (var i = 0, len = ElementArray.length; i < len; i++) {
             ElementArray[i].appendChild(ChildArray[i]);
         }
     })(jsonData, detailsElement);
 })();
于 2012-12-26T23:04:01.387 回答