Such simple code, why isn't it working? When the page loads, it should display an alert box that reads "ready".
<!DOCTYPE html>
<html>
<head>
<title>
Title
</title>
<script type="text/javascript">
$(document).ready(function() {
alert("ready");
});
</script>
</head>
<body>
Content
</body>
</html>
I feel like it's something incredibly obvious, but I'm at a point where I can't think straight.
I've tried both in the latest versions of Chrome and开发者_运维问答 Firefox.
Where's your jquery ref?
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
I had this problem, but jQuery was referenced. I had accidentally written the previous script tag as a self closing tag when moving my javascript to an external file:
<script type="text/javascript" src="/wherever/whatever.js" />
<script type="text/javascript">
$(document).ready(function(){ /* not hit */ });
</script>
The external reference tag can't be self-closing. It should read <script type="text/javascript" src="/wherever/whatever.js"></script>
You forgot to include jQuery, so $
is undefined.
One glance at the JS console in chrome was all it took to figure out. Whenever something JS wont go, your first check should always be the console to look for an error. Usually this will tell you exaxtly what's up.
very simple you are trying creating a jquery object without linking to jquery.
option one
link to jquery use jquery 1.6.1 (currently the latest) 1.4.x is an older version.
see here
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
option two
alert(); can be called without jquery, so remove the $(document).ready()
bit
I know this is an old issue, but I just had the same problem. I thought I'd add this for other jquery newbies. I did not find this answer on Stackoverflow. In my case, I had to change from:
<script src="jquery.min.js"></script>
<script src="lodash.min.js"></script>
<script src="app.js"></script>
to:
<script src="./jquery.min.js"></script>
<script src="./lodash.min.js"></script>
<script src="./app.js"></script>
Looks like jquery needs the correct relative path specified. In my case, the script tags are inside the index.html and the dir hierarchy is flat i.e
<dirname>
-- index.html
-- app.js
-- jquery.min.js
-- lodash.min.js
You have to include jquery before your jquery code...
You aren't referencing the jQuery library anywhere, so $(document).ready
will not work.
I had this problem when I had the vsdoc (Visual Studio intellisense script) included at runtime. Had to hide it at runtime like this:
@if(false){
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.0-vsdoc.js" type="text/javascript"></script>
}
精彩评论