jQuery: generating html
Is there a better way to do the following?
var html;
var i;
for (i=0; i < 4; i++) {
html += '<div class="row">';
html += '<span class="label">Dining Style:</span>';
html += '<span class="control">';
var j;
for (j=0; j < 3; j++){
html += '<div class="attribBox">';
html += '<ul>';
html += '<li><input type="checkbox" /> item 1</li>';
html += '</ul>';
html += '</div>';
}
html += '</span开发者_C百科>';
html += '<div class="clear"></div>';
}
$("#content").html(html);
Instead of creating the html, you can use jquery functions, available in 1.4+, to make you code much cleaner. Here is an example:
$("#content").append($("<div/>", {class : 'row'}).append($("<span/>", {class:'label', text : 'Dining Style'})))
The above code will generate:
<div class="row"><span class="label">Dining Style</span></div>
You still have to loop to create the three children but this is much cleaner.
Look here
http://api.jquery.com/category/plugins/templates/
and an overview
http://www.borismoore.com/2010/10/jquery-templates-is-now-official-jquery.html
I'm would use a templating system like iCanHaz.js
Append Child
Create Element
or most simply, innerHTML: http://www.tizag.com/javascriptT/javascript-innerHTML.php
精彩评论