What is the difference between these jQuery ready functions?

后端 未结 9 1516
名媛妹妹
名媛妹妹 2020-11-22 15:31

what is difference between

$(function(){

}); 

and

$(document).ready(function() { 

});
9条回答
  •  执念已碎
    2020-11-22 16:07

    The two are exactly equivalent: use whichever form you like.

    That said, I personally always use the expanded form $(document).ready(function(){}); for the simple reason that it is completely obvious what the code is doing. The approximate idea is that of "self-documenting code". Anyone coming to the code later on will immediately see that the code is to be run on the document's ready event. With the short-hand form, you have to rely upon the reader of your code understanding the meaning.

提交回复
热议问题