jQuery 的 hide() 和 show() 可以用來顯示和隱藏內(nèi)容。比如下面的例子:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>JQuery Demo</title>
<script src="scripts/jquery-1.9.1.js"></script>
<script>
$(document).ready(function () {
$("#hide").click(function () {
$("p").hide();
});
$("#show").click(function () {
$("p").show();
});
});
</script>
</head>
<body>
<p>If you click on the "Hide" button, I will disappear.</p>
<button id="hide">Hide</button>
<button id="show">Show</button>
</body>
</html>
http://wiki.jikexueyuan.com/project/jquery-tutorial/images/2.png" alt="" />
基本語法
$(selector).hide(speed,callback);
$(selector).show(speed,callback);
可選的參數(shù) speed 給出顯示或隱藏內(nèi)容的速度,可以使用 “slow”,”fast” 或者數(shù)字代表微秒。 第二個可選參數(shù)為回調(diào)函數(shù),在顯示或隱藏結(jié)束時調(diào)用。下面代碼在1秒鐘內(nèi)隱藏內(nèi)容:
$("button").click(function(){
$("p").hide(1000);
});
jQuery toggle()方法
使用 toggle()方法,可以實現(xiàn)交替顯示和隱藏內(nèi)容,如:
$("button").click(function(){
$("p").toggle();
});
其基本語法如下:
$(selector).toggle(speed,callback);
可選的參數(shù) speed 給出顯示或隱藏內(nèi)容的速度,可以使用“slow”,”fast”或者數(shù)字代表微秒。 第二個可選參數(shù)為回調(diào)函數(shù),在顯示或隱藏結(jié)束時調(diào)用