Replace the after () before () and remove () elements in jQuery

Source: Internet
Author: User


After () function

The after () method inserts the specified content after the selected element.

Before () function

The before () method inserts the specified content before the selected element.

Remove () function

The remove () method removes selected elements, including all text and subnodes.
This method does not delete matching elements from the jQuery object, so you can use these matching elements in the future.
Except for the element itself, remove () does not retain the jQuery data of the element. Other events such as bound events and additional data will be removed. This is different from detach.

Example: element replacement

The code is as follows: Copy code

/*
* Replace A with B
* After (B) inserts B to the end of A and returns the package set containing.
* Remove () delete the previous package set
**/
$ (A). after (B). remove ();

// Simple plug-in Encapsulation
$. Fn. replaceWidth (ele ){
Return this. after (ele). remove ();
}

Add two examples

1. replaceWith ()

Replace the selected content with the content in the brackets.

The code is as follows: Copy code

$ ("# Div"). replaceWith ("<div id =" div2 "> div2 </div> ");

Use the above method to replace the element with ID div with the DIV element with ID div2.
 
2. replaceAll ()
Replace the selected content with the selector in the brackets.

The code is as follows: Copy code

$ ("<Div> replaced content </div>"). replaceAll ("p ");

Use the above method to replace all p tags with the selected DIV tags.

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.