国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

jQuery中文參考手冊(cè) / jQuery - 刪除元素

jQuery - 刪除元素

jQuery - 刪除元素


通過(guò) jQuery,可以很容易地刪除已有的 HTML 元素。


刪除元素/內(nèi)容

如需刪除元素和內(nèi)容,一般可使用以下兩個(gè) jQuery 方法:

  • remove() - 刪除被選元素(及其子元素)

  • empty() - 從被選元素中刪除子元素


jQuery remove() 方法

jQuery remove() 方法刪除被選元素及其子元素。

實(shí)例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("#div1").remove();
  });
});
</script>
</head>
<body>

<div id="div1" style="height:100px;width:300px;border:1px solid black;background-color:yellow;">

這是 div 中的一些文本。
<p>這是在 div 中的一個(gè)段落。</p>
<p>這是在 div 中的另外一個(gè)段落。</p>

</div>
<br>
<button>移除div元素</button>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例


jQuery empty() 方法

jQuery empty() 方法刪除被選元素的子元素。

實(shí)例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("#div1").empty();
  });
});
</script>
</head>
<body>

<div id="div1" style="height:100px;width:300px;border:1px solid black;background-color:yellow;">

這是 div 中的一些文本。
<p>這是在 div 中的一個(gè)段落。</p>
<p>這是在 div 中的另外一個(gè)段落。</p>

</div>
<br>
<button>清空div元素</button>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例


過(guò)濾被刪除的元素

jQuery remove() 方法也可接受一個(gè)參數(shù),允許您對(duì)被刪元素進(jìn)行過(guò)濾。

該參數(shù)可以是任何 jQuery 選擇器的語(yǔ)法。

下面的例子刪除 class="italic" 的所有 <p> 元素:

實(shí)例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").remove(".italic");
  });
});
</script>
</head>
<body>

<p>這是一個(gè)段落。</p>
<p class="italic"><i>這是另外一個(gè)段落。</i></p>
<p class="italic"><i>這是另外一個(gè)段落。</i></p>
<button>移除所有  class="italic" 的 p 元素。</button>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例