Jquery - Is $ (document) already necessary?

I read the online tutorial, which states that if <script></script>the right of </body>, $(document).readyis not required b / c, the document has been loaded at this point.

Q1> Is this true?

Q2>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script src="jquery.viewport.min.js"></script>

<script>
$(window).scroll(function() { // this line will track all mouse scroll event

});
</script>

What does $ (window) mean? Is this a jquery selector? If so, then the previous statement looks correct, because we should not include this inside

$(document).ready(function() {

});

Q3> why are we using $ link here? why do we prefer to use $linkvar instead of the link?

<script>
$(window).scroll(function() {
  $link = $('nav a[hash=#first]');
  $link.addClass('selected');
});
</script>

thank

+5
source share
3 answers

Q1. . , jQuery - , , , , .

Q2. jQuery, . jQuery, $(document), $(document.body) - node jQuery, .

Q3. . $link = $('nav a[hash=#first]');, / $link, $('nav a[hash=#first]'), jQuery - / , . var $link = $('nav a[hash=#first]');, , $link - (- ).

; , DOM, ( , jQuery ), , ( ).

+2

Q1. api doc:

JavaScript , , , , .... , CSS, .

Inserting code before (or after) the closing body tag and not using .ready () usually works fine, because by the time the browser parser reaches the end of the body, dom will be complete enough for you to start working with selectors, etc.

Q2 "window" is an object exposed by the browser ; it is part of the DOM, but there is no need to refer to it in the style of .ready (function () {}), because the loaded HTML will not change this object in any way to affect its .scroll event.

+1
source

All Articles