如何在 HTML 元素中使用 jQuery 新增 display:none?


要在 jQuery 元素中使用 display: none 的解決方法,請使用 hide() 方法。它會執行相同的工作。

示例

你可以嘗試執行以下程式碼,瞭解如何在 HTML 元素中新增 display:none −

線上示例

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

<h1>Heading 1</h1>

<p style="font-size:15px">This is demo text. This will hide on button click.</p>
<p>This is another text. This will hide on button click</p>

<button>Hide</button>

</body>
</html>

更新於:17-Feb-2020

4K+ 檢視

開啟 職業生涯

完成課程,獲得認證

立即開始
廣告
© . All rights reserved.