[英]'td' - element not appending to 'tr' element using loop
I am dynamically creating a table with 3 col based. 我正在动态创建一个基于3 col的表。 I am using this code, But the td
is not appending to tr
. 我正在使用此代码,但是td
没有追加到tr
。
Here is my code : 这是我的代码:
var l = 10;
for(i=0; i<l; i++){
if(i%3==0){
var tr = $('tbody').append('<tr/>')
}
$(tr).append('<td>'+i+'</td>'); //not appending to tr element!
}
Change to: 改成:
var tr = $('<tr />').appendTo('tbody'); // this returns the tr element.
$('tbody').append('<tr/>')
returns the <tbody>
's jQuery
. $('tbody').append('<tr/>')
返回<tbody>
的jQuery
。
You should do this: 你应该做这个:
var body = $('tbody'),
l = 10,
tr,
i;
for (i = 0; i < l; i += 1) {
if (i % 3 === 0) {
tr = $('<tr />').appendTo(body);
}
tr.append('<td>' + i + '</td>');
}
Try: 尝试:
var i, il, tr, td;
var tbody = $('tbody');
for (i = 0, il = 10; i < il; i++) {
if (i % 3 === 0) {
tr = $('<tr />').appendTo(tbody);
}
$('<td />').text(i).appendTo(tr);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.