开发者

An easy way to generate HTML from top to bottom using jquery

I am trying to process a JSON response and generate HTML from it. I w开发者_开发百科ould like to construct HTML "top to bottom":

entry = document.createElement('div');
entry.class = "Entry";
entry_title = document.createElement('div');
entry_title.appendChild(document.createTextNode('My entry title'));
// and so on

I would like to use jquery's HTML parsing capabilities to simplify the code. But I can't find a way to do this in jquery - its append method returns the calling object, but not the created one, so I'll have to make one more select to get it.

So the question is - how can I simplify the code above using jquery?


var entry = $('<div>').addClass('Entry').
append( 
    $('<div>').text('My entry title') 
)
.appendTo('body')

in reverse:

var entry = $('<div>').text('My entry title')
.wrap( 
    $('<div>').addClass('Entry') 
)
.parent().appendTo('body')


Try this:

$('body').append(
  $('<div>').addClass('Entry').append(
    $('<div').text('My entry title')
  )
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜