簡體   English   中英

jQuery如何隱藏被單擊的單個html對象?

[英]jquery How do I hide a single html object being clicked on?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM