0

我想通过 insertAfter 方法创建一个带有 p 标签的 div 标签

所以现在我的代码是

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

现在的输出是这样的

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

如何在不重复insertAfter方法的情况下使输出变成这样

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

谢谢你

4

2 回答 2

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

演示--> http://jsfiddle.net/PCQrP/3/

于 2013-06-17T17:10:12.970 回答
1

我的方法需要先创建父元素,然后附加子元素:

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

http://jsfiddle.net/dvz7M/

于 2013-06-17T17:30:18.357 回答