0

所以我一直在浏览一些关于 HTML/CSS 和 Jquery 的 codeacademy 内容。

试图让我的代码在我的本地机器上运行时遇到了一些麻烦。

这是我的 HTML

<!DOCTYPE html>
<html>
    <head>
        <title>Krypton Redux</title>
        <link rel='stylesheet' type='text/css' href='stylesheet.css'/>
        <script type='text/javascript' src='script.js'></script>
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>    </head>

    <body>

        <div></div>
        <div style = "margin:10"></div>
        <div></div>
    </body>
</html>

这是我的 jQuery

$(document).ready(function(){
    $('div').draggable();
});

当我直接在我的 HTML 文件中编写 jQuery 代码然后在 Chrome 中打开它时,它可以工作。但是,在 .js 文件中执行此操作时,我收到一条错误消息,指出“$”无法识别。有人知道为什么吗?

4

2 回答 2

3

添加script.js下面的jQuery脚本。

<link rel='stylesheet' type='text/css' href='stylesheet.css'/>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type='text/javascript' src='script.js'></script>
于 2013-06-04T19:55:16.820 回答
3

$jQuery 别名。如果您$在脚本中使用 jQuery,则必须在脚本之前定义 jQuery。这是一个简单的修复。像这样重新排列你的脚本链接:

<!-- Load jQuery first -->
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

<!-- Since you use jQuery UI in your script as well, load it second. -->
<!-- Note however that jQuery Core must be included before UI because it's a dependency -->
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

<!-- Finally, load your script -->
<script type='text/javascript' src='script.js'></script>

希望这可以帮助。

于 2013-06-04T19:58:59.367 回答