简体   繁体   English

javascript文档准备功能

[英]javascript document ready function

And once again I am stuck in the learning process. 再一次,我被困在学习过程中。 I am trying to animate a background of a site using the help provided here . 我正在尝试使用此处提供的帮助为网站背景制作动画。 But I am a little stuck. 但是我有点卡住。 As I am teaching myself javascript (to replace basic actionscript). 当我在自学javascript时(代替基本的动作脚本)。 I like to write line by line instead of copying an pasting so I can understand how things work. 我喜欢逐行编写而不是复制粘贴,这样我才能理解事情的发展。

This is what I have so far: 这是我到目前为止的内容:

<!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>

As you can see the alert window should pop up as the function is started, but it doesn't. 如您所见,警报窗口应在该功能启动时弹出,但不是。 Is there a reason why this happens or should I just set up a body onLoad function to handle what I want to do when the page loads? 有这种原因的原因吗?还是我应该设置一个主体onLoad函数来处理页面加载时要执行的操作?

You forgot to include the jQuery javascript API in your page. 您忘记了在页面中包含jQuery javascript API。 It should be included before you use the $() function (which is an alias for the jQuery() function in this case.) 在使用$()函数之前,它应该包含在内jQuery()在这种情况下,它是jQuery()函数的别名。)

If you check your browser's Javascript console you probably have an exception for trying to use undefined $ . 如果检查浏览器的Javascript控制台,则可能会出现尝试使用未定义$的异常。 (In IE a handy trick while doing web development is to enable the Advanced option for "Display a notification for every script error," but this can get annoying when visiting other sites because lots of developers are lousy about identifying and fixing unhandled JS exceptions! Modern browsers usually use 'F12' (in the US at least), to open the developer tools for debugging Javascript, etc.) (在IE中,进行Web开发时的一个小技巧就是启用“显示每个脚本错误的通知”的“高级”选项,但是当访问其他站点时,这会变得很烦人,因为许多开发人员对于识别和修复未处理的JS异常并不满意!现代浏览器通常使用“ F12”(至少在美国使用)来打开开发人员工具来调试Java脚本等)

Corrected code: 更正的代码:

<!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>

This example uses the Google-hosted jQuery API, but you may also choose to download jQuery from http://jquery.com 本示例使用Google托管的jQuery API,但您也可以选择从http://jquery.com下载jQuery。

Demo 演示版

http://jsfiddle.net/dvADs/ http://jsfiddle.net/dvADs/

You are missing library reference~! 您缺少图书馆参考书〜! like this 像这样

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

Hope rest feeds your needs :) 希望休息能满足您的需求:)

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

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

$(document).ready(function(){
    window.alert("function started");
});

You are not first loading jQuery. 您不是先加载jQuery。 jQuery is a library that you are trying to call by using the $. jQuery是您尝试使用$调用的库。 You can download it here: http://jquery.com/download/ . 您可以在此处下载它: http : //jquery.com/download/ Make sure you load jQuery before the javascript code. 确保在javascript代码之前加载jQuery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM