日期:2014-05-16  浏览次数:20625 次

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('d