简体   繁体   English

$(document).ready in javascript

[英]$(document).ready in javascript

I am new to javascript and jquery. 我是javascript和jquery的新手。 I want to use $(document).ready to register some event handlers. 我想使用$(document).ready注册一些事件处理程序。 For example, code like this, and my question is what javascript libraries do I need to refer to at the head of the page in order to use $(document).ready? 例如,这样的代码,我的问题是,为了使用$(document).ready,我需要在页面顶部引用哪些javascript库? Appreciate if anyone could provide me a simple easy to use sample to learn $(document).ready. 感谢有人可以为我提供一个简单易用的示例来学习$(document).ready。

<script>$(document).ready(function()
{
// function implementation internals
});
</script>

thanks in advance, George 预先感谢乔治

All you need is the jQuery library. 您需要的只是jQuery库。

http://www.jquery.com http://www.jquery.com

You can either download the library and include it from your own server or you could use one of the many CDN's which provide the library. 您可以下载该库并从您自己的服务器中包含它,也可以使用提供该库的许多CDN之一。 For instance: 例如:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

<script type="text/javascript">
     $(document).ready(function() {
           // do something useful
     });
</script>

Google keeps copies of a bunch of libraries on their servers, which are pretty reliable. Google在其服务器上保留了一堆库的副本, 这些副本非常可靠。

Just add the following to your <head> section, and place your snippet somewhere below. 只需将以下内容添加到您的<head>部分,并将您的代码段放在下面的某处。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>

In summary, 综上所述,

Put the <script> tag provided by sje397 in the <head> section of the page, which provides the only library you need... jQuery. 将sje397提供的<script>标签放在页面的<head>部分中,该标签提供了您唯一需要的库... jQuery。

(Alternatively: <script src="http://code.jquery.com/jquery-1.4.4.js" type="text/javascript"></script> ) (或者: <script src="http://code.jquery.com/jquery-1.4.4.js" type="text/javascript"></script>

Read http://docs.jquery.com/Tutorials:How_jQuery_Works 阅读http://docs.jquery.com/Tutorials:How_jQuery_Works

And you should be good to go. 而且您应该很好。

<html>
<head>
</head>
<body>

<div id="someElement">Click Me</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    (function($) {
        $(document).ready(function() {

            $('#someElement').bind('click', function(event) {
                // event.preventDefault(); // you might want to do this if your event handler has a default action associated with it (e.g. a link that gets clicked with an href)
                // do stuff on your event (change click to whatever you need)
            });

        });
    })(jQuery);
</script>
</body>
</html>

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

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