2014-04-03 108 views
1

我写了一个简单的html文件来测试按钮,当单击时调用alert(),但我看不到任何响应。任何人都可以告诉我我做错了什么?这太奇怪了。jQuery功能没有响应

<!DOCTYPE html> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
    <title>Login Form</title> 
    <!-- <link rel="stylesheet" href="css/signupstyle.css"> --> 

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
    <script> 

     $("#btnSubmit").click(function(){ 
      alert("button"); 
      console.log("button"); 
     }); 

     $("#facebook").click(function(){ 
      alert("button"); 
      console.log("button"); 
     });  


</script> 
</head> 
<body> 
    <input id="btnSubmit" type="submit" value="Release"/> 
    <button id="facebook">Sign up with Facebook</button> 

</body> 

+0

...或地方的''的结束。 – raina77ow

+0

只需要注意**在结束''标签!! ** –

回答

2

你需要添加的document.ready功能,并写在它所有的事件:

$(document).ready(function(){ 
    $("#btnSubmit").click(function(){ 
     alert("button"); 
     console.log("button"); 
    }); 

    $("#facebook").click(function(){ 
     alert("button"); 
     console.log("button"); 
    });  
}); 

或这样做:

$(function(){ 
    $("#btnSubmit").click(function(){ 
     alert("button"); 
     console.log("button"); 
    }); 

    $("#facebook").click(function(){ 
     alert("button"); 
     console.log("button"); 
    });  
}); 
1

包住所有jQuery选择一个的document.ready功能

$(document).ready(function(){ 
    $("#btnSubmit").click(function(){ 
     alert("button"); 
     console.log("button"); 
    }); 

    $("#facebook").click(function(){ 
     alert("button"); 
     console.log("button"); 
    });  
}); 

还有其他的方法中,但htis应该工作。

+0

之前放置脚本,并且我可以在body标记正确之前在脚本中添加此代码? – ueg1990

+0

@ ueg1990是的,只是将它添加到头部。更好地练习这种方式 – sourRaspberri

-1

您正试图在加载DOM之前运行该脚本,其他答案中的一个不同选项是在DOM元素之后执行脚本。

<!DOCTYPE html> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
    <title>Login Form</title> 
    <!-- <link rel="stylesheet" href="css/signupstyle.css"> --> 

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 

</head> 
<body> 
    <input id="btnSubmit" type="submit" value="Release"/> 
    <button id="facebook">Sign up with Facebook</button> 
     <script> 

     $("#btnSubmit").click(function(){ 
      alert("button"); 
      console.log("button"); 
     }); 

     $("#facebook").click(function(){ 
      alert("button"); 
      console.log("button"); 
     });  


</script> 
</body> 
1

你应该换你的代码在$(文件)。就绪(函数(){你的代码放在这里})