简体   繁体   中英

jQuery: Use insertAfter method to create p tag inside div tag

I want to create a div tag with p tag inside it by insertAfter method

So now My code is

$('<p></p>', {
  'width': '110',
  'height': '110',
}).insertAfter($(this));

The output now like this

<p style="width:110px;height:110px;"></p>

How to make the output to be like this without repeating insertAfter method

<div id="wrapP"><p style="width:110px;height:110px;"></p></div>

Thank You

$('<p></p>', {
  'width': '110',
  'height': '110',
}).insertAfter($(this)).wrap('<div id="wrapP"></div>');

Demo --> http://jsfiddle.net/PCQrP/3/

My approach requires creating the parent element first, and then appending the child:

$('<div id="wrapP" />').append($('<p></p>', {
  'width': '110',
  'height': '110',
})).insertAfter('body');

http://jsfiddle.net/dvz7M/

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