Which JQuery document.ready is better? [duplicate]

╄→гoц情女王★ 提交于 2019-12-12 16:44:51

问题


Possible Duplicate:
jQuery $( function() {} ) and $(document).ready the same?

Do you know which one is better and why?

The first one;

$(document).ready(function() {
  // your code
});

The second One :

$(function() {
  // your code
});

回答1:


It doesn't matter. I'm more of a fan of the 2nd case because its easier to type.

This is what the function does internally.

// HANDLE: $(function)
// Shortcut for document ready
} else if ( jQuery.isFunction( selector ) ) {
   return rootjQuery.ready( selector );
}



回答2:


They are equivalent. It depends on how verbose or concise you want to be.




回答3:


All three of the following syntaxes are equivalent:

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

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




回答4:


Both are the same reference : http://api.jquery.com/ready/



来源:https://stackoverflow.com/questions/6332760/which-jquery-document-ready-is-better

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!