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、父子关系的添加 //主动添加 $('#shu').append('<li>关羽</li>');//往后添加 $('#shu').prepend('<li>黄忠</li>');//往前添加 //被动添加 $('<li>黄盖</li>').appendTo($('#wu')); $('<li>陆逊</li>').prependTo($('#wu')); //添加已存节点,发生位置改变 $('#shu').prepend($('#wu li:eq(1)')); //2、兄弟关系的添加.after .before .insertAfter .insertBefore //主动 $('#liu').after('<li>赵云</li>'); $('#liu').before('<li>诸葛亮</li>'); //被动 $('<li>黄月英</li>').insertAfter($('#liu')); $('<li>孙尚香</li>').insertBefore($('#liu'));
2. Replace node .replaceAll .replaceWith
//replaceAll主动替换 $('#wu').replaceAll($('#shu')); //replaceWith被动替换 $('#yu').replaceWith('<li>黄盖</li>');
3 , delete node.empty() .remove()
$('#wu').empty(); //清空对应的子节点 $('#fei').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
$('#fei').clone(false); //只复制节点本身 $('#fei').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!