问题
what are the differences (if any) for the following jQuery document ready functions:
$("document").ready(function() {});
and $(function() {});
回答1:
They are equivalent, the later is a shorthand form for the first.
From the jQuery documentation:
All three of the following syntaxes are equivalent:
$(document).ready(handler) $().ready(handler) (this is not recommended) $(handler)
回答2:
The only difference is brevity. http://api.jquery.com/jQuery/#jQuery3
jQuery( callback )
This function behaves just like
$(document).ready()
, in that it should be used to wrap other$()
operations on your page that depend on the DOM being ready. While this function is, technically, chainable, there really isn't much use for chaining against it.
(emphasis added)
回答3:
They are the same. Check out: http://api.jquery.com/jQuery/#jQuery3
回答4:
They're the same; as stated earlier, the latter is merely a shorthand version of the former.
I prefer using the expanded version, as I feel it makes code easier to read.
回答5:
Its the same. Different names to do the same thing
回答6:
$(afunc)
just calls $(document).ready(afunc);
after 4 if statements which don't get entered since afunc is a function.
回答7:
$(document).ready(function(){})
$().ready(function(){}) (this is not recommended)
$(function(){})
来源:https://stackoverflow.com/questions/7479817/jquery-document-ready-functions