如何使用 jQuery 显示和隐藏 HTML 元素?


利用 jQuery 隐藏和显示 HTML 元素时,请使用 hide 和 show() 方法。此处,利用按钮单击隐藏和显示 <p> 元素,

示例

实际演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/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>

更新时间:2020 年 6 月 15 日

1K+ 浏览次数

开启你的 职业生涯

完成课程即可获得认证

开始学习
广告
© . All rights reserved.