简体   繁体   English

如何在TR中包含多个td?

[英]How to include multiple td into the tr?

I just wanted to append td in the tr 我只是想在tr附加td

Table

<table class="u-full-width" id="result">
  <thead>
    <tr>
      <th>Project Name</th>
      <th>Id</th>
      <th>Event</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Dave Gamache</td>
      <td>26</td>
      <td>Male</td>
    </tr> 
  </tbody>
</table>

Script 脚本

// Receive Message 
socket.on('message', function(data){
  console.log(data);
  var Project = data.project;
  var Id = data.id;
  var Event = data.event;

  var tr = document.createElement("tr");
  var td1 = tr.appendChild(document.createElement('td'));
  var td2 = tr.appendChild(document.createElement('td'));
  var td3 = tr.appendChild(document.createElement('td'));

  td1.innerHTML = Project;
  td2.innerHTML = Id;
  td3.innerHTML = Event;

  document.getElementById("result").appendChild(td1, td2, td3);
});

I come up with this above code but this is not working means see the image... 我想出了上面的代码,但这不起作用,意味着看到图片...

在此处输入图片说明

The last line is wrong. 最后一行是错误的。 You should append the tr to the table, not all the td s. 您应该将tr而不是所有td附加到表中。

document.getElementById("result").appendChild(tr);

document.getElementById("result").appendChild(td1, td2, td3); document.getElementById(“ result”)。appendChild(td1,td2,td3);

seems you are appending your tds directly to the table, not a tr in the tbody. 看来您是将tds直接附加到表上,而不是tbody中的tr。

Try this way to bind your data 尝试这种方式绑定您的数据

<table class="u-full-width" id="result">
  <thead>
    <tr>
      <th>Project Name</th>
      <th>Id</th>
      <th>Event</th>
    </tr>
  </thead>
  <tbody id="bodytable">     
  </tbody>
</table>



    socket.on('message', function(data){
      console.log(data); 
      var _html='';
      html +='<tr>'
      html +='<td>'+data.project+'</td>';
      html +='<td>'+data.id+'</td>';
      html +='<td>'+data.event+'</td></tr>';   
      $('#bodytable').append(_html);
      });    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM