开发者

$(document).ready in javascript

I am new to javascript and jquery. I want to use $(document).ready to register some event handlers. 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? Appreciate if anyone could provide m开发者_JAVA技巧e a simple easy to use sample to learn $(document).ready.

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

thanks in advance, George


All you need is the jQuery library.

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

Just add the following to your <head> section, and place your snippet somewhere below.

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

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

Read 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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜