如何在jQuery中隐藏页面上的所有标题元素,当它们被点击时
在这篇文章中,我们将看到如何用jQuery隐藏页面上的所有标题元素,当它们被点击时。为了隐藏页面上的所有标题元素,我们使用slideUp()方法。首先我们使用click()方法来检测元素是否被点击,然后使用slideUp()方法来隐藏标题元素。
语法:
$(selector).click(function() {
$(this).slideUp();
})
在这里,我们使用click()方法来启动点击事件,或附加一个函数在点击事件发生时运行,并使用slideUp()方法来隐藏所选元素。
示例:
<!DOCTYPE html>
<html>
<head>
<title>
How to hide all heading elements on a page
when they are clicked in jQuery?
</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
(document).ready(function () {
("h1, h3").click(function () {
$(this).slideUp();
});
});
</script>
<style>
body {
text-align: center;
}
h1 {
color: green;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h3>
How to hide all heading elements on a page
<br>when they are clicked in jQuery?
</h3>
</body>
</html>
输出: