javascript - 如何使用javascript填充html表格而不使用div?

标签 javascript jquery html-table

我正在尝试使用 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/

相关文章:

javascript - 如何在 NodeJS 中显示图像而不从 url 下载图像?

javascript - Rails 的 ActionCable 服务器端使用参数订阅

javascript - 使用 AngularJS 的搜索建议

css - 涉及 thead 和 div 的重叠元素

php - 为什么我不能在 HTML 表格中指定单元格的宽度和高度?

javascript - 数组和 For 循环 JS 的逻辑问题

javascript - 滚动后粘性社交分享

jquery - 如何使其成为完全可点击的 DIV

javascript - 使用 Object-Fit 时的悬停缩放效果 : Cover

javascript - jquery 选择下一个 tbody 所有行