使用示例講解 jQuery detach()


jQuery 中的 detach() 方法用於移除選定的元素。

語法

語法如下 −

$(selector).detach()

示例

現在我們來看一個實現 jQuery detach() 方法 的示例 −

 即時演示

<!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").detach();
      });
   });
</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>

輸出

這將會產生以下輸出 −

點選“移除元素” −

更新於:31-Dec-2019

164 次瀏覽

開啟你的 職業生涯

透過學習課程獲得認證

開始學習
廣告
© . All rights reserved.