我正在尝试使用javascript从json填充html表(我的表有3列),但数据永远不会使用div方法填充!可以告诉我如何使用不使用div来填充表格行的内容吗?
for(i in json)
{
var div = "<tr id=\""+i+"\">\n" +
"<td>"+i+"</td>\n" +
"<td><img src=\""+ json[i].thumb +"\" height=\"42\" width=\"42\"></td>\n" +
"<td>\n" +
"<a href=\"javascript:callfunction('Name=" + json[i].title + "','" + json[i].sources + "','"+ json[i].thumb +"')\" onclick=\"selectLink(this);\">" + json[i].title + "</a><br> \n" +
"<br></td></tr>\n\n";
$("#myDiv").append(div);
}
待填表:
<table id="list" cellspacing="0" border="1">
<tr>
<th>Item#</th>
<th>Logo</th>
<th>Description</th>
</tr>
<div id='myDiv'></div>
</table>
答案 0 :(得分:3)
这是你的新表:
<table id="list" cellspacing="0" border="1">
<thead>
<tr>
<th>Item#</th>
<th>Logo</th>
<th>Description</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
代码:
var html = '';
for(var i in json)
{
html += '<tr id="'+i+'">';
html += '<td>'+i+'</td>';
html += '<td><img src="'+json[i].thumb+'" height="42" width="42"></td>';
html += "<td>";
html += "<a href=\"javascript:callfunction('Name=" + json[i].title + "','" + json[i].sources + "','"+ json[i].thumb +"')\" onclick=\"selectLink(this);\">" + json[i].title + "</a><br/><br/>";
html += '</td>';
html += '</tr>';
}
$('#list > tbody').html(html);