The DOM operation of jquery Development II

Source: Internet
Author: User

The next article

3, inserting nodes
The methods for inserting nodes are as follows:

4, deleting nodes
(1) Remove () method
The effect is to remove all matching elements from the DOM. The sample code is as follows:

$li =$("ul li:eq(1)").remove();       //获取第二个<li>节点后,将它从网页中删除。

Note: When a node is removed with remove (), all descendant nodes that the node contains are deleted at the same time. The return value of this method is a reference to the deleted node, so you can continue to use the elements later.

(2) Detach () method
The detach () and the Remove () method also remove the matching elements from the DOM, but be aware that this method does not remove the matching elements from the jquery object, so that the matching elements can be used in the future. Unlike remove (), all the bound events, the attached data will be preserved.
The sample code is as follows:

$li =$("ul li:eq(1)").detach();  //获取第二个<li>节点后,将它从网页中删除。

(3) Empty () method
Strictly speaking, the empty () method is not to delete a node, but to empty the node, which empties all the descendant nodes in the element.
The sample code is as follows:

var$li = $("ul li:eq(1)").empty();  //获取第二个<li>节点后,清空元素里的内容。

5, copy Node

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" ><html><head><meta http-equiv="Content-type" Content="text/html; charset=gb18030 "><title>CSS Ajax</title><script type ="text/javascript" src ="Jquery.js " ></script><script type ="Text/javascript">$ (document). Ready (function(){ $ ("ul Li:eq (2)"). Click (function   (){ $ (this). Clone (). AppendTo ("ul"); //Copy the node that is currently clicked and append it to the <ul> element.     });});</script></head><body><p Title ="Choose your favorite Fruit">Choose your favorite fruit</P><ul><Li title ="Apple">Apple</li><Li title ="Orange">Orange</li><Li title ="Pineapple">Pineapple</li></ul></body></html>

6, node substitution
If you are replacing a node, jquery provides the appropriate method, namely ReplaceWith () and ReplaceAll ();
The function of the ReplaceWith () method is to replace all matching elements with the specified HTML live DOM element.
The sample code is as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" ><html><head><meta http-equiv="Content-type" Content="text/html; charset=gb18030 "><title>JQuery Test</title><script type ="text/javascript" src ="Jquery.js " ></script><script type ="Text/javascript">$ (document). Ready (function(){ $ ("P"). ReplaceWith ("<strong> Which fruit do you dislike the most? </strong> ")});</script></head><body><p Title ="Choose your favorite Fruit">Choose your favorite fruit</P><ul><Li title ="Apple">Apple</li><Li title ="Orange">Orange</li><Li title ="Pineapple">Pineapple</li></ul></body></html>

The results of the operation are as follows:

The sample code for the ReplaceAll () method is as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" ><html><head><meta http-equiv="Content-type" Content="text/html; charset=gb18030 "><title>JQuery Test</title><script type ="text/javascript" src ="Jquery.js" ></script><script type ="Text/javascript">$ (document). Ready (function(){ $ ("<li> lychee </li>"). ReplaceAll ( "Li")});</script></head><body><p Title ="Choose your favorite Fruit">Choose your favorite fruit</P><ul><Li title ="Apple">Apple</li><Li title ="Orange">Orange</li><Li title ="Pineapple">Pineapple</li></ul></body></html>

The results of the operation are as follows:

7, Parcel node
(1) Wrap () method
The jquery code example is as follows:

$("strong").wrap("<br></br>"//用<br>标签将<strong>元素包裹起来

The results of the operation are as follows:

<br><srong>something</strong></br>

(2) Wrapall () method
The method wraps all matching elements with one element, unlike the Wrap () method, which wraps all the elements.
The jquery sample code is as follows:

$("strong").wrapAll("<br></br>");

The results of the operation are as follows:

<br><strong>你最喜欢的水果</strong><strong>你最不喜欢的水果</strong></br>

(3) Wrapinner () method
The method wraps the word content of each matching element (the text node) with its structured markup. The jquery sample code is as follows:

$("strong").wrapInner("<br></br>");

The results of the operation are as follows:

<strong><br>最喜欢的水果</br></strong>

The DOM operation of jquery Development II

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.