简体   繁体   中英

jQuery Document Ready function syntax

Is there a difference between this:

$(document).ready(function() {

and this:

$().ready(function() {

Thank you.

according to jquery documentation they are the same.

All three of the following syntaxes are equivalent:

$(document).ready(handler)
$().ready(handler) // this is not recommended
$(handler)

i personally feel using $(document).ready(handler) makes it more readable though.

They both are equivalent but the later one is not recommended per jQuery docs.

http://api.jquery.com/ready/

If I am not completely mistake, the first one is what you wanna use in any case (when using non-intrusive JS). The second one might even work (not tested) but if it does it will certainly be slower, as jQuery would have to detect the object that is loaded and upon which to run the denoted functon.

First, it has nothing to do with PHP, that's javascript code (using the jQuery library). I retagged your question accordingly.

Now, these 3 variants do the same thing (attach an event handler to the DOMLoaded event):

$(function(){});
$(document).ready(function(){});
$().ready(function(){});

The third one is not recommended, according to jQuery docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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