如何使用jQuery隐藏标签?

时间:2015-04-29 01:17:18

标签: jquery html5

**<!DOCTYPE html>
<html lang="en">
<head>
  <title>welcome to Wall Street model</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#c").click(function(){
    $("h1").hide();
});
</script>

</head>  

<button id="c">click to hide</button>

<h1>this is a message</h1>
</body>
</html>**

我是jQuery的新手。我尝试使用此代码在单击按钮时隐藏h1标签。但它不起作用。

2 个答案:

答案 0 :(得分:1)

缺少});可以在fiddle

中找到演示
<script>
$(document).ready(function(){
  $("#c").click(function(){
    $("h1").hide();
  });
});
</script>

答案 1 :(得分:1)

编写代码时,需要确保 all 语法正确无误:

$(document).ready(function(){
    $("#c").click(function(){
        $("h1").hide();
});

在这里你忘记了});。它应该是:

$(document).ready(function(){
    $("#c").click(function(){
        $("h1").hide();
    });
});

您的JavaScript无效,因为当浏览器遇到一个问题时,它将立即停止执行JavaScript。

JSBin