【问题标题】:New to jquery, wondering why .hide() not workingjquery 新手,想知道为什么 .hide() 不起作用
【发布时间】:2015-01-21 20:07:28
【问题描述】:

我真的不知道为什么这不起作用,但我是 javascript 新手,所以我确定我犯了一个错误。

我有一个按钮,单击时我希望 div 消失但是当我单击按钮时没有任何反应。

$(document).ready(function() {
	$('#begin-button').click(function() {
		$('#welcome-div').hide();
	});
};
<div id="welcome-div">
    <p>Welcome</p>
 </div>

<a href="#" id="begin-button" class="intro-button"/>Begin Tour</a>

【问题讨论】:

  • 缺少 jquery 库
  • 还有一个错字在结尾});中缺少)

标签: javascript jquery


【解决方案1】:
<!doctype html>
<html lang="en">    
<head>
  <meta charset="utf-8">
  <title>Hide</title>
  <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  <style type="text/css">
  label input[type="file"] 
{
display: block;
margin-top: -20px;
opacity: 0;
}
  </style>
  <script>
   $(window).ready(function() {
$(document).delegate('#hide','click',function(){

  $('#spanFileName').hide();
});
});
  </script>
</head>
<body>
<p id='hide'> Click for hide</p>

<span id='spanFileName'>Hide me</span>
</body>
</html>

【讨论】:

    【解决方案2】:

    您的代码 sn-p 中缺少 jQuery 库。添加 &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt; 以包含 jQuery 库

    $(document).ready(function() {
        $('#begin-button').click(function() {
            $('#welcome-div').hide();
        });
    };
    

    $(document).ready(function() {
      $('#begin-button').click(function() {
        $('#welcome-div').hide();
      });
    });
    //^....... missing ')'
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <div id="welcome-div">
      <p>Welcome</p>
    </div>
    
    <a href="#" id="begin-button" class="intro-button" />Begin Tour</a>

    【讨论】:

      猜你喜欢
      • 2014-10-25
      • 2013-06-25
      • 2016-11-25
      • 1970-01-01
      • 2013-05-14
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多