jquery: Choosing a document.ready method

坚强是说给别人听的谎言 提交于 2019-12-02 00:52:49

问题


I'm new to jquery. I've just read that these 2 are equivalent:

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

$(function() {});

Which one is better practice, or more accepted? The first one strikes me as clearer in that it states the "document.ready" part - but then again, I'm new to jquery. It could be that to anyone with any experience in jquery, the second option just as clearly implies "document.ready". Which of these options should I choose?


回答1:


Feel free to use either one of them. Just as you think, the latter version is a shorthand for $(document).ready().

The first option was available since the beginning of jQuery releases as oppose to the second option.

Personally I prefer the second version for it is shorter.




回答2:


All three of the following syntaxes are equivalent:

$(document).ready(handler);

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

$(handler);


来源:https://stackoverflow.com/questions/7307509/jquery-choosing-a-document-ready-method

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