本文示範如何使用 jQuery 的 hide() 方法,隱藏 id 為 test 的元素。
<!DOCTYPE html>
<html>
<head>
<script src="js/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p id="test">This is another paragraph.cnblogs.com</p>
<button>Click me</button>
</body>
</html>
以 id 選擇器 $("#test") 精準選到單一元素並將其隱藏。由於 id 在頁面中理應唯一,因此這種方式很適合用來操作特定的某個元素。希望本文對大家的 jQuery 開發有所幫助。