jquery:选择 document.ready 方法
我是 jquery 新手。我刚刚读到这两个是等效的:
$(document).ready(function() {});
$(function() {});
哪一个是更好的实践,还是更容易被接受?第一个让我印象更清楚,因为它指出了“document.ready”部分 - 但话又说回来,我是 jquery 的新手。对于任何有 jquery 经验的人来说,第二个选项可能明确意味着“document.ready”。我应该选择这些选项中的哪一个?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请随意使用其中任何一个。正如您所想,后一个版本是
$(document).ready()
的简写。第一个选项自 jQuery 发布之初就可用,与第二个选项相反。
我个人更喜欢第二个版本,因为它更短。
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.
以下所有三种语法都是等效的:
All three of the following syntaxes are equivalent: