【问题标题】:Javascript page not linking to my HTML FileJavascript 页面未链接到我的 HTML 文件
【发布时间】:2013-03-08 01:14:40
【问题描述】:

我最初只是使用 JSFiddle,但我想将我的项目转移到 Notepad++,以便我可以在我的计算机上保存这些文件。问题是,我页面上的 Javascript 没有生效。

标题代码:

<head>
<link href="C:/HTML/App/app.css" type="text/css" rel="stylesheet" />
<script type='text/javascript' src='C:/HTML/App/app.js'></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
</head>

有什么理由吗?它可以很好地链接到我的样式表,但我的 app.js 似乎无法正常工作。

【问题讨论】:

  • 是否有机会从 file:// 运行?
  • 你检查路径“C:/HTML/App/app.js”是否存在app.js?
  • 等等,app.js 是否包含需要 jquery 的东西?
  • 是的,它包含需要 jquery 的东西。

标签: javascript jquery html head


【解决方案1】:

三件事:

app.js 使用 jQuery,所以需要先包含 jQuery 文件

另外,你说你在你的电脑上运行这个。您可以使用 localhost,但如果您从 file:// 运行,src="//... 将无法工作,因为您的浏览器将寻找 file://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js 而不是 http://ajax.googleapis.com/ajax/libs/ jqueryui/1.9.1/jquery-ui.min.js
所以你需要做的就是添加http:

但是你的意思是 jQuery 而不是 jQuery UI? (感谢@ahren)

<head>
    <link href="C:/HTML/App/app.css" type="text/css" rel="stylesheet" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <!-- <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script> -->
    <script type='text/javascript' src='C:/HTML/App/app.js'></script>
</head>

【讨论】:

  • 我在我的电脑上获取这些文件,所以如果我没有你
  • script 和 src 很好,你需要 http:// 而不仅仅是 ajax.googleapis.com 中的 //...如果你从 file:// 运行,则调用,否则你的浏览器会寻找 @ 987654326@ 这不太可能存在。
  • 为什么要包含jQueryUI而不包含jQuery?
  • 谢谢@ahren。我什至没有注意到它。更新了答案。
  • 非常感谢大家。另外,我也在使用 UI,所以我把评论去掉了。一切正常。
【解决方案2】:

jquery 先出现,像这样:

<head>
    <link href="C:/HTML/App/app.css" type="text/css" rel="stylesheet" />
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
    <script type='text/javascript' src='C:/HTML/App/app.js'></script>
</head>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-05
    • 2021-08-21
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    相关资源
    最近更新 更多