[jQuery] $(document).ready(function()) versus $(function())
Hi, another beginner question (who probably is missing some crucial
point):
I do not understand the difference between $
(document).ready(function()) and $(function()).
Consider the following code (linked to the jQuery lib):
<html>
<head>
<script type="text/javascript">
$(function(){
alert('anonymous function here')
});
$(document).ready(function(){
alert('document ready here')
});
</script>
</head>
<body>
...
<div class="vb" id="demo"></div>
<script type="text/javascript">alert('end doc here');</script>
</body>
</html>
Then we see three alerts in the following order: 1. end doc;
2.anonymous fucntion 3. documentready
So the following question springs to mind:
why use $(document).ready() ?
$(function()) seems to work just the same?
none of them has any problems accessing the dom tree, for instance
reading $('#demo').attr('class')