jQuery:将 td 元素添加到 tr

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19710010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:28:37  来源:igfitidea点击:

jQuery: Adding td element to a tr

javascriptjqueryhtml

提问by Himanshu Yadav

Using a rowvariable to get data dynamically and set the table row.

使用row变量动态获取数据并设置表格行。

var row = '<tr>'+
      '<td>'+obj.Message+'</td>'+
      '<td>'+obj.Error+'</td>'+
      '<td>'+obj.Detail+'</td>'+
      '</tr>';

Based a value I tried to add another <td>to the row object but it didn't work.

基于一个值,我尝试向<td>行对象添加另一个值,但没有成功。

if(obj.Type=='Error') {
  $(row).find('td:last').append('<td>'+ obj.ErrorCode+'</td>');
}
$('table> tbody:last').append(row);

First I tried to just append the <td>to row but that didn't work as well.

首先,我尝试将<td>行附加到行,但这并没有奏效。

$(row).append('<td>'+ obj.ErrorCode+'</td>');

回答by azz

You're not storing the last row.

您没有存储最后一行。

When you call $(row).find(...).append(...)the result is not being stored in a variable. The best solution is probably keeping a jQuery object from the start:

当您调用时$(row).find(...).append(...),结果并未存储在变量中。最好的解决方案可能是从一开始就保留一个 jQuery 对象:

//         v--- use a jQuery object here
var $row = $('<tr>'+
      '<td>'+obj.Message+'</td>'+
      '<td>'+obj.Error+'</td>'+
      '<td>'+obj.Detail+'</td>'+
      '</tr>');    
if(obj.Type=='Error') {
    $row.append('<td>'+ obj.ErrorCode+'</td>');
}
$('table> tbody:last').append($row);

See fiddle

小提琴

回答by PSL

Instead of appenduse afterand use ===(or ==) instead of =

代替append使用after并使用===(or ==) 代替=

  if(obj.Type === 'Error') {
    $(row).find('td:last').after('<td>'+ obj.ErrorCode+'</td>'); //This will add the new td after the last one
   ..

or just append it to the row:

或者只是将它附加到行:

   $(row).append('<td>'+ obj.ErrorCode +'</td>'); //this will do the same thing, less code and append this as last td of your row.

回答by Shoaib Chikate

You have done mistake here:

你在这里做错了:

 obj.Type='Error' 

(=)act as assignment operator here.

(=) 在这里充当赋值运算符。

Check with

检查

  if(obj.Type=='Error'){
   //hence code.
  }

回答by Dhaval Marthak

use ==comparision operator for comparision: To insert new cell use .after() or you can just append whole row using .append()

使用==比较运算符进行比较:要插入新单元格使用 .after() 或者您可以使用.append()

if(obj.Type=='Error') {
   $(row).append('<td>'+ obj.ErrorCode+'</td>');
}

回答by tymeJV

Your appending INSIDE a td, you want .after

你的附加 INSIDE a td,你想要.after

if(obj.Type=='Error') {
    $(row).find('td:last').after('<td>'+ obj.ErrorCode+'</td>');
}

And the whole, ==over =is an issue as well.

总的来说,==结束=也是一个问题。

回答by MonkeyZeus

Don't you have to add rowto the DOM before you can search it and add another <td>?

row在搜索和添加另一个 DOM 之前,您不必添加到 DOM<td>吗?

Solution 1

解决方案1

var row = '<tr>'+
          '<td>'+obj.Message+'</td>'+
          '<td>'+obj.Error+'</td>'+
          '<td>'+obj.Detail+'</td>';

if(obj.Type=='Error') {
    row += '<td>'+ obj.ErrorCode+'</td>';
}

row += '</tr>';

$('table> tbody:last').append(row);

Solution 2

解决方案2

var row = '<tr>'+
          '<td>'+obj.Message+'</td>'+
          '<td>'+obj.Error+'</td>'+
          '<td>'+obj.Detail+'</td>'+
          '</tr>';

$('table').append(row);

if(obj.Type=='Error') {
    $('table').find('td:last').after('<td>'+ obj.ErrorCode+'</td>');
}

Solution 3

解决方案3

var row = '<tr>'+
          '<td>'+obj.Message+'</td>'+
          '<td>'+obj.Error+'</td>'+
          '<td>'+obj.Detail+'</td>'+
          '</tr>';

$('table').append(row);

if(obj.Type=='Error') {
    $('table tr').append('<td>'+ obj.ErrorCode+'</td>');
}