admin管理员组

文章数量:1355617

I keep having this issue with many different code snippets in JQuery where the code I try works on sites like JSFiddle and w3schools, but doesn't work on my puter when I just try to load an html file through Chrome.

My question is, to run JQuery locally, do you need piling software like you would need with C, or should it work like HTML/CSS does with just a browser?

Here's an example of some simple code that works in JSFiddle and w3schools but doesn't work for me...

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").hide();
  });
});
</script>
</head>

<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>

Additionally, if a piler is required. Can you remend one to me? Thanks for all your help!

I keep having this issue with many different code snippets in JQuery where the code I try works on sites like JSFiddle and w3schools, but doesn't work on my puter when I just try to load an html file through Chrome.

My question is, to run JQuery locally, do you need piling software like you would need with C, or should it work like HTML/CSS does with just a browser?

Here's an example of some simple code that works in JSFiddle and w3schools but doesn't work for me...

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").hide();
  });
});
</script>
</head>

<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>

Additionally, if a piler is required. Can you remend one to me? Thanks for all your help!

Share Improve this question edited May 17, 2013 at 0:00 user207421 311k44 gold badges321 silver badges489 bronze badges asked May 16, 2013 at 18:53 SeslynSeslyn 83710 silver badges19 bronze badges 4
  • 1 A piler isn't necessary, but I would guess you're opening the file directly, rather than using a webserver on your local machine. This confuses Chrome, which uses the file: protocol to load jQuery, which is wrong as it should be HTTP. You can fix this by specifying http:// in your script src, or by using a webserver. – Matt Commented May 16, 2013 at 18:56
  • What are the issues you are seeing? Any errors in the console or failed requests in the network tab? If you are trying to do things that involve canvas, FileReader, or other more "advanced" HTMl5-ish things, they typically don't work on the file:/// protocol. To get around that I typically just do python -m SimpleHTTPServer in my root directory and then navigate to http://localhost:8000 – Matt Greer Commented May 16, 2013 at 18:57
  • possible duplicate of Loading jquery from google doesn't work (for me) – Ram Commented May 16, 2013 at 18:58
  • possible duplicate of Why does no jQuery work on my home machine? – acdcjunior Commented May 16, 2013 at 19:08
Add a ment  | 

2 Answers 2

Reset to default 8

Ok, using the // syntax doesn't work when you're running through the local file system - it looks for file://ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js

Obviously that file doesn't exist and jQuery isn't loaded on the page. You need to specify it as an HTTP(s) resource if you're going to open it locally:

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

You need to use http: like this -

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

本文标签: JavascriptJquery Doesn39t Function LocallyStack Overflow