如何使用 jQuery 顯示和隱藏 HTML 元素?


若要使用 jQuery 隱藏和顯示 HTML 元素,請使用 hide 和 show() 方法。此處,<p> 元素會在單擊按鈕時隱藏和顯示,

示例

線上演示

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

<p>This text  will show on clicking "Show element" button, and hide on clicking "Hide element" button.</p>

<button id="hidden">Hide element</button>
<button id="visible">Show element</button>

</body>
</html>

更新於:15-6-2020

1K+ 瀏覽量

開啟你的職業生涯

透過完成本教程,獲得認證

開始
廣告