javascript - jquery 如何隐藏被点击的单个 html 对象?

标签 javascript jquery html

我正在 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/

相关文章:

javascript - 单击按钮即可下载图像数组

javascript - AngularJS Ui Router 正则表达式匹配 url 中的通配符

javascript - 如何在mean js中安装angular-xeditable

javascript - 在 JavaScript 中循环列表列表时出错

html - 当数据库中的值更改时,我如何刷新页面的一部分(Codeigniter)?

Javascript RegExp + 单词边界 + unicode 字符

javascript - FILE ["file1"] 不是通过 AJAX 设置的

html - 如何防止 float 的子 div 换行?

javascript - 如何解决 youtube 播放器未显示完整宽度和高度的问题?

javascript - "event.originalEvent.clipboardData.getData"是什么意思?