2

我在 JQuery 中的许多不同代码片段中一直遇到这个问题,我尝试的代码在 JSFiddle 和 w3schools 等网站上运行,但是当我尝试通过 Chrome 加载 html 文件时在我的计算机上不起作用。

我的问题是,要在本地运行 JQuery,您是否需要像使用 C 一样编译软件,还是应该像 HTML/CSS 那样只使用浏览器?

这是一些在 JSFiddle 和 w3schools 中工作但对我不起作用的简单代码示例......

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/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>

此外,如果需要编译器。你能推荐一个给我吗?感谢你的帮助!

4

2 回答 2

8

好的,当您通过本地文件系统运行时,使用 // 语法不起作用 - 它会查找file://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js

显然该文件不存在并且页面上没有加载 jQuery。如果要在本地打开它,则需要将其指定为 HTTP(s) 资源:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
于 2013-05-16T18:58:10.980 回答
4

你需要http:像这样使用 -

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
于 2013-05-16T18:58:09.673 回答