jQuery 中的 jQuery.hide() 和 jQuery.remove() 方法有什麼區別?


jQuery.hide()

如果你想隱藏一個元素,可以使用 hide() 方法來隱藏所選元素。

示例

你可以嘗試執行以下程式碼來了解如何使用 jQuery 的 jQuery.hide() 方法

即時演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $(".button1").click(function(){
        $("p").hide();
    });
    $(".button2").click(function(){
        $("p").show();
    });
});
</script>
</head>
<body>

<button class="button1">Hide the element</button>
<button class="button2">Show the element</button>
<p>This is demo text.</p>

</body>
</html>

jQuery.remove()

remove() 方法將刪除所選元素,但也包括文字和子節點。

示例

你可以嘗試執行以下程式碼來了解如何使用 jQuery 的 jQuery.remove() 方法

即時演示

<html>

   <head>
      <title>jQuery remove() method</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
       
      <script>
         $(document).ready(function() {
            $("div").click(function () {
               $(this).remove().appendTo("#result");
            });
         });
      </script>
       
      <style>
         .div {
             margin:10px;
             padding:12px;
             border:2px solid #666;
             width:60px;
         }
      </style>
   </head>
   
   <body>
   
      <p>Click on any square below to see the result:</p>
      <p id = "result"> THIS IS TEST </p>
      <hr />
       
      <div class = "div" style = "background-color:blue;">ONE</div>
      <div class = "div" style = "background-color:green;">TWO</div>
      <div class = "div" style = "background-color:red;">THREE</div>
       
   </body>
</html>

更新於:2019-12-10

400 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告