jQuery hide()方法
hide()是jQuery中的一个内置方法,用于隐藏所选元素。
语法:
$(selector).hide(duration, easing, call_function);
这里的选择器是指选定的元素。
参数:它接受三个参数,具体如下-
- duration。它规定了隐藏效果的速度。
- easing。它规定了元素在不同的动画点上的速度。
- call_function。这是隐藏操作后要执行的回调函数。
返回值:它不返回任何值。
jQuery例子显示hide()方法的工作:
例子1:在下面的代码中,没有使用参数传递给这个方法。
<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<!-- jQuery code to show the working of this method -->
<script>
(document).ready(function () {
(".b1").click(function () {
$("p").hide();
});
});
</script>
<style>
div {
width: 50%;
height: 80px;
padding: 20px;
margin: 10px;
border: 2px solid green;
font-size: 30px;
}
.b1 {
margin: 10px;
}
</style>
</head>
<body>
<div>
<p>GeeksforGeeks !.</p>
</div>
<!-- click on this button and
above paragraph will disappear -->
<button class="b1">Click me !</button>
</body>
</html>
输出:
例子2:在下面的代码中,参数被传递给这个方法。
<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<!--jQuery code to show the working of this method-->
<script>
(document).ready(function () {
(".btn1").click(function () {
$("p").hide(1000, function () {
alert("Hide() method has finished its working!");
});
});
});
</script>
<style>
p {
width: 40%;
padding: 20px;
height: 50px;
border: 2px solid green;
}
</style>
</head>
<body>
<p>GeeksforGeeks.!</p>
<!-- click on this button and
above paragraph will hide -->
<button class="btn1">Click to Hide</button>
</body>
</html>
输出: