最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQUERY won't work on local machine - Stack Overflow

programmeradmin2浏览0评论

I'm trying JQUERY on my machine, but for some reason, nothing seems to work. Here's the test file:

<html>
<head>
<script type="text/css" src="jquery.js">
</script>
<script type="text/javascript">
    $("p").mouseover(function () {
      $(this).css("color","black");
    });

    $(document).ready(function(){
$("body").css("background-color","black");
$("body").css("color","white");
});
</script>
</head>
<body>
<h1>This is a test</h1>
<p>Roll over me!</p>
</body>
</html>

Nothing in there works. Also, if anybody wants to know, accessing through my domain and through the local both don't work. I'm really confused, because I copied most of that code off the internet, just in case there was something wrong with my typing.

For some reason, firefox is throwing this error:

Code: Evaluate
$ is not defined
.html
Line: 6
$ is not defined
.html
Line: 6

New code (moved the p onmouseover handeler)

    <script src="jquery.js" type="text/css">
</script>
<script type="text/javascript">
$(document).ready(function(){
    $("p").mouseover(function () {
      $(this).css("color","black");
    });
$("body").css("background-color","black");
$("body").css("color","white");
});
</script>

I'm trying JQUERY on my machine, but for some reason, nothing seems to work. Here's the test file:

<html>
<head>
<script type="text/css" src="jquery.js">
</script>
<script type="text/javascript">
    $("p").mouseover(function () {
      $(this).css("color","black");
    });

    $(document).ready(function(){
$("body").css("background-color","black");
$("body").css("color","white");
});
</script>
</head>
<body>
<h1>This is a test</h1>
<p>Roll over me!</p>
</body>
</html>

Nothing in there works. Also, if anybody wants to know, accessing through my domain and through the local both don't work. I'm really confused, because I copied most of that code off the internet, just in case there was something wrong with my typing.

For some reason, firefox is throwing this error:

Code: Evaluate
$ is not defined
http://hussain.mooo.com/jq.html
Line: 6
$ is not defined
http://hussain.mooo.com/jq.html
Line: 6

New code (moved the p onmouseover handeler)

    <script src="jquery.js" type="text/css">
</script>
<script type="text/javascript">
$(document).ready(function(){
    $("p").mouseover(function () {
      $(this).css("color","black");
    });
$("body").css("background-color","black");
$("body").css("color","white");
});
</script>
Share Improve this question edited Jan 5, 2010 at 14:45 John Rasch 63.4k19 gold badges109 silver badges141 bronze badges asked Dec 24, 2009 at 22:46 HussainHussain 6632 gold badges16 silver badges27 bronze badges 3
  • 2 is jquery.js actually present? – Pekka Commented Dec 24, 2009 at 22:48
  • 2 Please revert your question, otherwise this defeats the purpose of asking and the whole SO model. – meder omuraliev Commented Dec 24, 2009 at 23:41
  • I had the same difficulty in my jquery code, till I found the solution to the problem in this forum's question : Jquery codes doesn't work if I'm using a local jquery.js file, why? It's an encoding problem, which disappears when you use the charset="UTF-8" attribute in the script tag of the .js .For details, visit the question mentioned above. – Sap Commented Oct 8, 2012 at 9:49
Add a comment  | 

4 Answers 4

Reset to default 22

Specify correct type for javascript file:

<script type="text/javascript" src="jquery.js"></script>

Update

You're currently using type="text/css" as content type for javascript file which is incorrect. Try to copy above code into your script.

Screenshot

removed dead ImageShack link

Install firebug and see what it tells you in the Console tab.

You should move the attachment of the mouseover handler into $(document).ready(...) because the paragraph won't necessarily exist until the document is ready and so no handler can be attached to it.

Download the latest version of jQuery "jquery-1.3.2.min.js" and link the file correctly. and try this,

<script type="text/javascript">
$(function(){
    $("p").mouseover(function () {
      $(this).css("color","black");
    });

    $("body").css("background-color","black");
    $("body").css("color","white");
});
</script>
发布评论

评论列表(0)

  1. 暂无评论