简体   繁体   中英

Create new DOM elements with children elements in jQuery

I want to create following elements

<span>
    <h3>Title</h3>
    <p>This is paragraph</p>
</span>

I want to do above using jquery in following manner.

$('<span />', {
}).appendTo('body');

How can I create children element of span with above method?

Fiddle Demo

$('<span />', {})
    .append($('<h3/>').text('Tite'))
    .append($('</p>').text('para'))
    .appendTo('body');


or

Fiddle Demo

 $('<span />', {}) .append(function () { return '<h3>Title</h3><p>This is paragraph</p>'; }).appendTo('body'); 

Fiddle Demo

 $('<span />', {}) .append($('<h3/>').text('Tite'), $('</p>').text('para')) .appendTo('body'); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM