Home >Web Front-end >JS Tutorial >Detailed explanation of the functions of adding, replacing, deleting and copying nodes using jQuery

Detailed explanation of the functions of adding, replacing, deleting and copying nodes using jQuery

小云云
小云云Original
2018-01-01 10:54:041723browse

This article mainly introduces jQuery to implement the append, replace, delete, and copy functions of nodes. It analyzes jQuery's operation skills for DOM nodes based on specific examples, including append(), prepend(), replaceAll(), and replaceWith(). Friends in need can refer to the tips for using empty(), remove(), clone(), clone() and other methods. I hope it can help everyone.

The example in this article describes how jQuery implements the functions of appending, replacing, deleting, and copying nodes. Share it with everyone for your reference, the details are as follows:

1. Adding nodes is divided into adding father-son and sibling nodes. Each method of appending nodes is divided into active append and passive append.


//1、父子关系的添加
//主动添加
$(&#39;#shu&#39;).append(&#39;<li>关羽</li>&#39;);//往后添加
$(&#39;#shu&#39;).prepend(&#39;<li>黄忠</li>&#39;);//往前添加
//被动添加
$(&#39;<li>黄盖</li>&#39;).appendTo($(&#39;#wu&#39;));
$(&#39;<li>陆逊</li>&#39;).prependTo($(&#39;#wu&#39;));
//添加已存节点,发生位置改变
$(&#39;#shu&#39;).prepend($(&#39;#wu li:eq(1)&#39;));
//2、兄弟关系的添加.after .before .insertAfter .insertBefore
//主动
$(&#39;#liu&#39;).after(&#39;<li>赵云</li>&#39;);
$(&#39;#liu&#39;).before(&#39;<li>诸葛亮</li>&#39;);
//被动
$(&#39;<li>黄月英</li>&#39;).insertAfter($(&#39;#liu&#39;));
$(&#39;<li>孙尚香</li>&#39;).insertBefore($(&#39;#liu&#39;));

2. Replace node .replaceAll .replaceWith


//replaceAll主动替换
$(&#39;#wu&#39;).replaceAll($(&#39;#shu&#39;));
//replaceWith被动替换
$(&#39;#yu&#39;).replaceWith(&#39;<li>黄盖</li>&#39;);

3 , delete node.empty() .remove()


$(&#39;#wu&#39;).empty(); //清空对应的子节点
$(&#39;#fei&#39;).remove(); //删除匹配到的节点

4. Copy node .clone(true) .clone(false)

If the parameter is true, the node and its events will be copied
If the parameter is false, only the node itself (including internal information) will be copied

$(&#39;#fei&#39;).clone(false); //只复制节点本身
$(&#39;#fei&#39;).clone(true); //复制节点和事件

Related recommendations:

A brief discussion on event bubbling, event delegation, and jQuery element node operations

ZTree asynchronous loading and expansion of the first-level node method implementation

Example sharing JQuery selector and DOM node operation exercises

The above is the detailed content of Detailed explanation of the functions of adding, replacing, deleting and copying nodes using jQuery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn