0

我又一次陷入了学习过程中。我正在尝试使用此处提供的帮助为网站的背景设置动画。但我有点卡住了。当我自学javascript(替换基本动作脚本)时。我喜欢逐行写而不是复制粘贴,这样我就可以理解事情是如何运作的。

这是我到目前为止所拥有的:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"><head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>My Site</title>
<script type="text/javascript">
    $(document).ready(function(){
        window.alert("function started");
    });
    </script>
    </head>

    <body>
    </body>
    </html>

正如你所看到的,当函数启动时应该弹出警告窗口,但它没有。发生这种情况是有原因的,还是我应该设置一个主体 onLoad 函数来处理页面加载时我想做的事情?

4

3 回答 3

3

您忘记在页面中包含 jQuery javascript API。在使用函数之前应该包含它$()(在这种情况下,它是函数的别名jQuery()。)

如果您检查浏览器的 Javascript 控制台,您可能会在尝试使用 undefined 时遇到异常$。(在 IE 中,在进行 Web 开发时,一个方便的技巧是启用“为每个脚本错误显示通知”的高级选项,但这在访问其他网站时会变得很烦人,因为许多开发人员对于识别和修复未处理的 JS 异常都很糟糕!现代浏览器通常使用“F12”(至少在美国),打开开发者工具来调试 Javascript 等)

更正的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>My Site</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function ()
        {
            window.alert("function started");
        });
    </script>
</head>
<body>
</body>
</html>

此示例使用 Google 托管的 jQuery API,但您也可以选择从http://jquery.com下载 jQuery

于 2013-10-24T21:38:46.407 回答
0

演示

http://jsfiddle.net/dvADs/

你缺少图书馆参考~!像这样

<script type='text/javascript' src='//code.jquery.com/jquery-2.0.2.js'></script>

希望休息满足您的需求:)

基础知识:http: //jqfundamentals.com/chapter/jquery-basics

JQ CDN:http: //jquery.com/download/

$(document).ready(function(){
    window.alert("function started");
});
于 2013-10-24T21:38:38.217 回答
0

您不是首先加载 jQuery。jQuery 是您尝试使用 $ 调用的库。你可以在这里下载它:http: //jquery.com/download/。确保在 javascript 代码之前加载 jQuery。

于 2013-10-24T21:40:06.857 回答