returning new set from jquery's .wrap()
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.
精彩评论