一:节点的添加
内部插入
- append(content|fn):会将元素添加到指定元素内部的最后
- appendTo(content):会将元素添加到指定元素内部的最后
- prepend(content|fn):会将元素添加到指定元素内部的最前面
- prependTo(content):会将元素添加到指定元素内部的最前面
外部插入
- after(content|fn):会将元素添加到指定元素外部的后面
- before(content|fn):会将元素添加到指定元素外部的前面
- insertAfter(content):会将元素添加到指定元素外部的后面
- insertBefore(content):会将元素添加到指定元素外部的前面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> </style> <script src="../jquery/jquery-3.4.1.min.js"></script> <script> $(function () { $('button').click(function () { //创建一个节点 var $li = $('<li>新增的li</li>'); //放字符串里,会创建li节点 /*内部插入*/ // $('ul').append($li); // $li.appendTo('ul'); // $('ul').prepend($li); // $li.prependTo('ul'); /*外部插入*/ // $('ul').after($li); $li.insertAfter('ul'); // $('ul').before($li); // $li.insertBefore('ul'); }) }); </script> </head> <body> <button>添加节点</button> <ul> <li>我是第1个li</li> <li>我是第2个li</li> <li>我是第3个li</li> </ul> <div>我是div</div> </body> </html>
二:节点的删除
删除
- empty() :删除指定元素的内容和子元素,指定元素自身不会被删除
- remove([expr]):删除指定元素
- detach([expr]):删除指定元素
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> </style> <script src="../jquery/jquery-3.4.1.min.js"></script> <script> $(function () { $('button').click(function () { // $('div').remove(); // $('li').remove('.item'); // $('div').detach(); // $('li').detach('.item'); $('div').empty(); }); }); </script> </head> <body> <button>删除节点</button> <ul> <li class="item">我是第1个li</li> <li>我是第2个li</li> <li class="item">我是第3个li</li> <li>我是第4个li</li> <li class="item">我是第5个li</li> </ul> <div>我是div <p>我是段落</p> </div> </body> </html>
三:节点替换
替换
- replaceWith(content|fn):替换所有匹配的元素为指定的元素
- replaceAll(selector):替换所有匹配的元素为指定的元素,写法和上面不同
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> </style> <script src="../jquery/jquery-3.4.1.min.js"></script> <script> $(function () { $('button').click(function(){ //创建节点 var $p=$('<p>我是段落</p>'); // $('li').replaceWith($p); $p.replaceAll('span'); }) }); </script> </head> <body> <button>替换节点</button> <ul> <li>我是第1个li</li> <li>我是第2个li</li> <li>我是第3个li</li> </ul> <span>我是标签</span> </body> </html>
四:复制节点
如果传入false则是浅复制,浅复制只复制元素
如果传入true则是深复制,深复制不仅会复制元素,还会复制元素的事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> </style> <script src="../jquery/jquery-3.4.1.min.js"></script> <script> $(function () { $('button').eq(0).click(function(){ var $li=$('li:first').clone(false); $('ul').append($li); }) $('button').eq(1).click(function(){ var $li=$('li:first').clone(true); $('ul').append($li); }) $('li').click(function(){ alert($(this).html()); }) }); </script> </head> <body> <button>浅复制</button> <button>深复制</button> <ul> <li>我是第1个li</li> <li>我是第2个li</li> <li>我是第3个li</li> <li>我是第4个li</li> <li>我是第5个li</li> </ul> </body> </html>