帶示例的 jQuery remove()


jQuery 中的 remove() 方法用於刪除選定的元素。

語法

語法如下 −

$(selector).remove(selector)

以上,parameter selector 用於指定要刪除的一個或多個元素。

示例

現在讓我們看一個示例來實現 jQuery remove() 方法 

 即時演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("button").click(function(){
         $("span").remove();
      });
   });
</script>
<style>
span {
   background-color: red;
   color: white;
}
</style>
</head>
<body>
<h2>Demo Heading</h2>
<p>This is a <span>demo</span> text.</p>
<button>Remove element</button>
</body>
</html>

輸出

這將生成以下輸出 −

現在,單擊“Remove element”刪除紅色的 span 元素。單擊“Remove element”按鈕後 −

更新於: 2019 年 12 月 31 日

99 次檢視

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.