我正在尝试使用 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>
最佳答案
这是您的新表:
<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);
关于javascript - 如何使用javascript填充html表格而不使用div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39885528/