admin管理员组文章数量:1024615
menuOrder
is an empty div
$("#menuOrder").append('<table>');
$(menus).each(function(i, menu) {
$("#menuOrder").append('<tr><td>');
$("#menuOrder").append(menu.text);
$("#menuOrder").append('</td><td>');
if (i > 0) {
$("#menuOrder").append('<img src="/images/_images/up.png" />');
} else {
$("#menuOrder").append('<img src="/images/_images/blank.png" />');
}
if (i < menus.length - 1) {
$("#menuOrder").append('<img src="/images/_images/down.png" />');
}
$("#menuOrder").append('</td>');
$("#menuOrder").append('</tr>');
});
$("#menuOrder").append('</table>');
this code not work properly, how can I change it with minimum iterations?
menuOrder
is an empty div
$("#menuOrder").append('<table>');
$(menus).each(function(i, menu) {
$("#menuOrder").append('<tr><td>');
$("#menuOrder").append(menu.text);
$("#menuOrder").append('</td><td>');
if (i > 0) {
$("#menuOrder").append('<img src="/images/_images/up.png" />');
} else {
$("#menuOrder").append('<img src="/images/_images/blank.png" />');
}
if (i < menus.length - 1) {
$("#menuOrder").append('<img src="/images/_images/down.png" />');
}
$("#menuOrder").append('</td>');
$("#menuOrder").append('</tr>');
});
$("#menuOrder").append('</table>');
this code not work properly, how can I change it with minimum iterations?
Share Improve this question edited May 28, 2017 at 15:15 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 26, 2010 at 21:10 kusanagikusanagi 14.6k22 gold badges89 silver badges112 bronze badges 1- 2 It is remend to avoid lots of .append like this, instead when possible, build the string with HTML and then append – Fabiano Soriani Commented Apr 26, 2010 at 21:12
2 Answers
Reset to default 4Try doing something like this:
var rows = [];
$(menus).each(function(i, menu) {
var row = '<tr><td>'.menu.text.'</td><td>';
if (i > 0) {
row +='<img src="/images/_images/up.png" />';
}
else {
row +='<img src="/images/_images/blank.png" />';
}
if (i < menus.length - 1) {
row +='<img src="/images/_images/down.png" />';
}
row += '</td></tr>';
rows.push(row);
});
$('<table></table>').append(rows.join('')).appendTo('#menuOrder');
This fills the rows
array with a string that contains the HTML for each row. When its done creating all the rows then it creates a jQuery Object of a table element and appends all the rows to it. It then appends the table to #menuOrder
.
Note that with append()
you can't do this:
$("#menuOrder").append('</table>');
Instead of glueing together pieces of HTML strings, you must think in terms of plete DOM nodes. Examples of valid arguments for append()
:
.append($('.something'))
.append(document.createElement('div'))
.append('<div />') // automatically creates empty div element
.append('<tr><td></td></tr>') // automatically creates tr with td inside
menuOrder
is an empty div
$("#menuOrder").append('<table>');
$(menus).each(function(i, menu) {
$("#menuOrder").append('<tr><td>');
$("#menuOrder").append(menu.text);
$("#menuOrder").append('</td><td>');
if (i > 0) {
$("#menuOrder").append('<img src="/images/_images/up.png" />');
} else {
$("#menuOrder").append('<img src="/images/_images/blank.png" />');
}
if (i < menus.length - 1) {
$("#menuOrder").append('<img src="/images/_images/down.png" />');
}
$("#menuOrder").append('</td>');
$("#menuOrder").append('</tr>');
});
$("#menuOrder").append('</table>');
this code not work properly, how can I change it with minimum iterations?
menuOrder
is an empty div
$("#menuOrder").append('<table>');
$(menus).each(function(i, menu) {
$("#menuOrder").append('<tr><td>');
$("#menuOrder").append(menu.text);
$("#menuOrder").append('</td><td>');
if (i > 0) {
$("#menuOrder").append('<img src="/images/_images/up.png" />');
} else {
$("#menuOrder").append('<img src="/images/_images/blank.png" />');
}
if (i < menus.length - 1) {
$("#menuOrder").append('<img src="/images/_images/down.png" />');
}
$("#menuOrder").append('</td>');
$("#menuOrder").append('</tr>');
});
$("#menuOrder").append('</table>');
this code not work properly, how can I change it with minimum iterations?
Share Improve this question edited May 28, 2017 at 15:15 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 26, 2010 at 21:10 kusanagikusanagi 14.6k22 gold badges89 silver badges112 bronze badges 1- 2 It is remend to avoid lots of .append like this, instead when possible, build the string with HTML and then append – Fabiano Soriani Commented Apr 26, 2010 at 21:12
2 Answers
Reset to default 4Try doing something like this:
var rows = [];
$(menus).each(function(i, menu) {
var row = '<tr><td>'.menu.text.'</td><td>';
if (i > 0) {
row +='<img src="/images/_images/up.png" />';
}
else {
row +='<img src="/images/_images/blank.png" />';
}
if (i < menus.length - 1) {
row +='<img src="/images/_images/down.png" />';
}
row += '</td></tr>';
rows.push(row);
});
$('<table></table>').append(rows.join('')).appendTo('#menuOrder');
This fills the rows
array with a string that contains the HTML for each row. When its done creating all the rows then it creates a jQuery Object of a table element and appends all the rows to it. It then appends the table to #menuOrder
.
Note that with append()
you can't do this:
$("#menuOrder").append('</table>');
Instead of glueing together pieces of HTML strings, you must think in terms of plete DOM nodes. Examples of valid arguments for append()
:
.append($('.something'))
.append(document.createElement('div'))
.append('<div />') // automatically creates empty div element
.append('<tr><td></td></tr>') // automatically creates tr with td inside
本文标签: javascriptjQuery build tableStack Overflow
版权声明:本文标题:javascript - jQuery build table - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745560175a2156116.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论