我正在 W3Schools 上做 jquery 教程。我出于好奇和学习而问,仅此而已。没有业务应用程序。
我想单击一个 HTML 元素并仅使用一个函数将其隐藏。
我点击 (h1,h2,p),但似乎我需要将标签本身传递给函数以在使用 $(this) 时隐藏它。
我试过了
$(*).click(function(){
但这隐藏了一切。我的代码是...
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$([the HTML object I'm clicking on now]).click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
如果你点击我,我就会消失。
点我离开!
也点我吧!
最佳答案
您必须引用要操作的对象,然后使用 this
关键字,将函数应用于指定(单击)的元素。
$('.box, input, .btn').click(function() {
$(this).hide();
});
.box {
width: 50px;
height: 50px;
background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='box'></div>
<input class='input'>
<button class='btn'>click</button>
关于javascript - jquery 如何隐藏被点击的单个 html 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42428783/