移除元素

 方法 说明
remove()从DOM中移除匹配的元素及其所有后代和文本节点。
detach()从DOM中移除匹配的元素及其所有后代和文本节点。还会在内存中保留副本。通常使用remove()方法。
empty()从DOM中移除匹配元素子节点及后代。
unwrap()移除匹配结果的父节点,并保留匹配元素。

remove 示例,移除全部元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!DOCTYPE html>
< html >
     < head >
         < meta charset = "UTF-8" >
         < title >remove</ title >
         </ script >
     </ head >
     < body >
         < div style = " font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; padding: 0px !important; background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important;">>
             < ul id = "ul" >
                 < li id = "a" >鼠标</ li >
                 < li id = "b" >键盘</ li >
                 < li id = "c" >屏幕</ li >
                 < li id = "d" >< a >主机</ a ></ li >
             </ ul >
         </ div >
 
         < button >移除元素</ button >
         < script >
             $('button').on('click', function () {
                $('div').remove();
             });
         </ script >
     </ body >
</ html >

remove 示例,移除指定的元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!DOCTYPE html>
< html >
     < head >
         < meta charset = "UTF-8" >
         < title >remove</ title >
         </ script >
     </ head >
     < body >
         < div style = " font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; padding: 0px !important; background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important;">>
             < ul id = "ul" >
                 < li id = "a" >鼠标</ li >
                 < li id = "b" >键盘</ li >
                 < li id = "c" >屏幕</ li >
                 < li id = "d" >< a >主机</ a ></ li >
             </ ul >
         </ div >
 
         < button >删除元素</ button >
         < script >
             $('button').on('click', function () {
                $('li').remove('#a');
             });
         </ script >
     </ body >
</ html >

需要指定到具体的才能移除哦。

detach()示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!DOCTYPE html>
< html >
     < head >
         < meta charset = "UTF-8" >
         < title >detach</ title >
         </ script >
     </ head >
     < body >
         < div style = " font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; padding: 0px !important; background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important;">>
             < ul id = "ul" >
                 < li id = "a" >鼠标</ li >
                 < li id = "b" >键盘</ li >
                 < li id = "c" >屏幕</ li >
                 < li id = "d" >< a >主机</ a ></ li >
             </ ul >
         </ div >
  
         < button >移除元素</ button >
         < script >
             $('button').on('click', function () {
                $('div').detach();
             });
         </ script >
     </ body >
</ html >

empty示例,清除所有元素文本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!DOCTYPE html>
< html >
     < head >
         < meta charset = "UTF-8" >
         < title >empty</ title >
         </ script >
     </ head >
     < body >
         < div style = " font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; padding: 0px !important; background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important;">>
             < ul id = "ul" >
                 < li id = "a" >鼠标</ li >
                 < li id = "b" >键盘</ li >
                 < li id = "c" >屏幕</ li >
                 < li id = "d" >< a >主机</ a ></ li >
             </ ul >
         </ div >
 
         < button >删除元素</ button >
         < script >
             $('button').on('click', function () {
                $('li').empty();
             });
         </ script >
     </ body >
</ html >

unwrap() 示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!DOCTYPE html>
< html >
     < head >
         < meta charset = "UTF-8" >
         < title >unwrap</ title >
         </ script >
     </ head >
     < body >
         < div style = " font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; padding: 0px !important; background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important;">>
             < ul id = "ul" >
                 < li id = "a" >鼠标</ li >
                 < li id = "b" >键盘</ li >
                 < li id = "c" >屏幕</ li >
                 < li id = "d" >< a >主机</ a ></ li >
             </ ul >
         </ div >
  
         < button >移除元素</ button >
         < script >
             $('button').on('click', function () {
                $('ul').unwrap();
             });
         </ script >
     </ body >
</ html >

转载于:https://www.cnblogs.com/max-hou/p/9151869.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值