开发者

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

开发者 https://www.devze.com 2022-12-09 16:33 出处:网络
I am trying to process a JSON response and generate HTML from it. I w开发者_开发百科ould like to construct HTML \"top to bottom\":

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

精彩评论

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