开发者

returning new set from jquery's .wrap()

开发者 https://www.devze.com 2023-03-13 08:27 出处:网络
jQuery\'s .wrap() method returns the original set of elements for chaining purposes. What is the most elegant way to return the original set of elements + the wrapped el开发者_如何学运维ements inste

jQuery's .wrap() method returns the original set of elements for chaining purposes.

What is the most elegant way to return the original set of elements + the wrapped el开发者_如何学运维ements instead?


If I understand you correctly, you'll need parent() and andSelf().

  • parent() will retrieve the parent for each element in the collection.
  • andSelf() will add the former collection to the new collection.

$("div.elements-to-wrap").wrap('<div class="wrapper" />').parent().andSelf();

Now, you have a new collection that contains both div.elements-to-wrap and all the div.wrapper that wrap the former elements.

0

精彩评论

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