如何在HTML中包含数组内容? [英] How to include array content in HTML?

查看:98
本文介绍了如何在HTML中包含数组内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码

var users = result.users; // array

$("#dialog:ui-dialog").dialog("destroy");

$("#dialog-confirm").dialog({
    resizable: false,
    height: 140,
    modal: true,
    open: function() {
    $(this).children('div.dialog-text').replaceWith("<b>New text goes here</b>");
    },

    buttons: {
    "Okay": function() {
        $(this).dialog("close");
    },
    Cancel: function() {
        is_okay = 0;
        $(this).dialog("close");
    }
    }
});

其中数组包含

{"ss":"Sandra Schlichting","fn":"Full name"}

我想要完成的是以(为了便于阅读而插入空格)的形式获取数组的内容

What I am trying to accomplish is to get the content of the arrays in the form of (white space inserted for readability)

<table>
  <tr>  <td>Initials</td>  <td>Full Name         </td>  </tr>
  <tr>  <td>ss      </td>  <td>Sandra Schlichting</td>  </tr>
  <tr>  <td>fn      </td>  <td>Full name         </td>  </tr>
</table>

并将其替换为< b>新文字到此处< / b> in

$(this).children('div.dialog-text').replaceWith("<b>New text goes here</b>");

据我所知 $。each()

但我仍然无法弄清楚如何开始。

But still I can't quite figure out how to get started.

任何人都可以帮助我吗?

Can anyone help me out?

推荐答案

function makeTable(users) {
    var result = '<table><tr><td>Initials</td><td>Full Name</td></tr>\n';
    $.each(users, function(index, value) {
        result += '<tr><td>' + index + '</td><td>' + value + '</td></tr>\n';
    });
    result += '</table>';
    return (result);
}

alert(makeTable({"ss":"Sandra Schlichting","fn":"Full name"}));




  • 在这里工作

  • $。每个 文档

    • Working here
    • $.each documentation
    • 这篇关于如何在HTML中包含数组内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆