vetty

jQuery remove()与jQuery empty()的区别

jQuery remove() 方法删除被选元素及其子元素。举例如下:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <script src="/jquery/jquery-1.11.1.min.js"></script>
 5 <script>
 6 $(document).ready(function(){
 7   $("button").click(function(){
 8     $("#div1").remove();
 9   });
10 });
11 </script>
12 </head>
13 
14 <body>
15 
16 <div id="div1" style="height:100px;width:300px;border:1px solid black;background-color:yellow;">
17 This is some text in the div.
18 <p>This is a paragraph in the div.</p>
19 <p>This is another paragraph in the div.</p>
20 </div>
21 
22 <br>
23 <button>删除 div 元素</button>
24 
25 </body>
26 </html>

点击按钮前如下:

点击按钮后:

remove()把整个div即选中的id部分的内容也已经删除。

 

jQuery empty() 方法删除被选元素的子元素。

把上面代码中的remove换成empty(),点击按钮后:

empty()并不清除选中的部分而是清除选中部分的子部分。

posted on 2015-02-03 16:26  cocos2014  阅读(309)  评论(0编辑  收藏  举报

导航