Shortcuts for jQuery's ready() method

后端 未结 3 1690
春和景丽
春和景丽 2020-12-12 20:45

I have seen some shortcuts for the ready() method and would like to know which actually happens first, because my test results confuse me..

$(document).ready         


        
3条回答
  •  醉酒成梦
    2020-12-12 21:19

    The third option is not a shortcut for .ready() (or jQuery related really), the self invoke runs immediately (as soon as it appears in the code), this is probably the shortcut you're thinking of though:

    $(function(){
      alert("I'm a ready shortcut");
    });
    

    Passing a function into $(func) is a shortcut for $(document).ready(func);. The no-conflict version would look like this:

    jQuery(function($) {
      //$ is jQuery
    });
    

提交回复
热议问题