当前位置: 代码迷 >> JavaScript >> js解析json,并动态创建dom元素,放到指定元素上,求
  详细解决方案

js解析json,并动态创建dom元素,放到指定元素上,求

热度:406   发布时间:2012-09-01 09:33:03.0
js解析json,并动态创建dom元素,放到指定元素下,求高手指点!
求高手急救!!!!
json数据如下:
var chat=[
  {
  'aa': '110000000',
  'chatLog': [
{'name1': 'abc','hour': '1111111111','content': '你好!'},
{'name1': 'efg','hour': '2222222222','content': '不好!'},
{'name1': 'efg','hour': '1111111111','content': '你好!'},
{'name1': 'efg','hour': '2222222222','content': '不好!'},
{'name1': 'abc','hour': '1111111111','content': '你好!'},
{'name1': 'efg','hour': '2222222222','content': '不好!'}
  ]
},
{
'aa': '11',
'chatLog': [
{'name1': 'abc','hour': '1111111111','content': '你好!'},
{'name1': 'efg','hour': '2222222222','content': '不好!'},
{'name1': 'abc','hour': '1111111111','content': '你好!'},
{'name1': 'efg','hour': '2222222222','content': '不好!'}
]
}
]

js部分为:
<script type="text/javascript">
$().ready(function() {
for(var i=0;i<chat.length;i++)
{
var div= document.createElement('div');
div.innerHTML='<span class="chat_time">'+chat[i].aa+'</span><hr/><dl></dl>'
div.className='chat_win';
$(".panal").append(div);
for(var j=0;j<chat[i].chatLog.length;j++)
{
var dt=document.createElement('dt');
$(".panal dl").append(dt);
}没有达到我要的效果???求修改!
 
}
});
</script>
<body>
<div class="panal"></div>
</body>



想实现成以下效果:
<div class="panal">
<div class="chat_win">
<span class="chat_time">110000000</span>
<hr/>
<dl>
<dt class="chat_A"><span>abc</span><span>1111111111</span></dt>
<dd>你好!</dd>
<dt class="chat_B"><span>efg</span><span>2222222222</span></dt>
<dd>不好!</dd>
<dt class="chat_B"><span>efg</span><span>1111111111</span></dt>
<dd>你好!</dd>
<dt class="chat_B"><span>efg</span><span>2222222222</span></dt>
<dd>不好!</dd>
<dt class="chat_A"><span>abc</span><span>1111111111</span></dt>
<dd>你好!</dd>
<dt class="chat_B"><span>efg</span><span>2222222222</span></dt>
<dd>不好!</dd>

</dl>
</div>
<div class="chat_win">
<span class="chat_time">11</span>
<hr/>
<dl>
<dt class="chat_A"><span>abc</span><span>1111111111</span></dt>
<dd>你好!</dd>
<dt class="chat_B"><span>efg</span><span>2222222222</span></dt>
<dd>不好!</dd>
<dt class="chat_A"><span>abc</span><span>1111111111</span></dt>
<dd>你好!</dd>
<dt class="chat_B"><span>efg</span><span>2222222222</span></dt>
<dd>不好!</dd>

</dl>
</div>
</div>


------解决方案--------------------
搞错,多了个new,代码出处了
JScript code
$().ready(function () {
    var div, dl, clsKV, chr = 65;
    for (var i = 0; i < chat.length; i++) {
        div = document.createElement('div');
        div.innerHTML = '<span class="chat_time">' + chat[i].aa + '</span><hr/><dl></dl>'
        div.className = 'chat_win';
        $(".panal").append(div);
        dl = $(div).find('dl');
        clsKV = {};
        chr = 65;
        for (var j = 0; j < chat[i].chatLog.length; j++) {//////////////
            if (!clsKV[chat[i].chatLog[j].name1]) {//样式控制
                ///clsKV[chat[i].chatLog[j].name1] = new String.fromCharCode(chr);

                clsKV[chat[i].chatLog[j].name1] = String.fromCharCode(chr);
                chr++;
            }
            dl.append('<dt class="chat_' + clsKV[chat[i].chatLog[j].name1] + '"><span>' + chat[i].chatLog[j].name1 + '</span><span>' + chat[i].chatLog[j].hour + '</span></dt><dd>' + chat[i].chatLog[j].content + '</dd>');
        }

    }
}); 
  相关解决方案