新操作符-jquery将当前元素封装在一个新的父容器中



我想使用jquery在当前元素周围封装一个新的父容器,类似的东西,但我不喜欢下面的行,我认为它们不是真正正确的或最佳实践:

this.before('<div id="container">');
this.after('</div>');
// do something with the new parent here
$("#container")...

有没有一种更自然的方法可以做到这一点?非常感谢。

您想要使用为此构建的包装函数!

this.wrap('<div id="container"></div>');

http://api.jquery.com/wrap/

是,.wrap()

示例:http://jsfiddle.net/ThiefMaster/rPhF2/

您也可以使用例如<div id="container"/> 来代替<div/>

最新更新