Easily grasp the use of wrap () and unwrap () functions in jquery _jquery

Source: Internet
Author: User

Wrap ()

The Wrap () function can accept any string or object that can be passed to the $ () factory function to specify a DOM structure. This structure can be nested several layers deep, but should contain only one core element. Each matching element will be wrapped in this structure. The method returns the original set of elements so that the chained method is used later.
eg

Appends a newline label to the current page and the specified HTML content
function W (html) {
  Document.body.innerHTML = "<br/>" + html;
}

var name = "Hello";
function foo (A, b) {
  w (this.name);
  W (a + b);
}
 Call
foo (1, 2) directly;
Hello
//3


var obj = {name: "Codeplayer", age:18};
var proxy = $.proxy (foo, obj, 5);
The agent invokes the Foo () function, at which point the inside of it points to object obj
proxy ();
Codeplayer
//
Run code


Unwrap ()
This function will move out of the element's parent element. This can quickly undo the effect of the. Wrap () method. Matching elements (and their sibling elements) will replace their parent elements on the DOM structure.
eg
Wrap each paragraph with a div with id "content"

# HTML Code:
<div>
  <p>Hello</p>
  <p>cruel</p>
  <p>world</ p>
</div>
 
# jQuery code:
 $ ("P"). Unwrap ()

Results:

<p>Hello</p>
<p>cruel</p>
<p>World</p>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.