我在某处看到过这样的代码片段:
$(document).ready(function(){
(function($){ $.fn.disableSelection = function() {
return this.attr('unselectable', 'on')
.css('user-select', 'none')
.on('selectstart', false); }; })(jQuery);
})
如你所知,这
$(document).ready(function(){
还有这个
(function($){
是一样的。那么程序员为什么要这样做呢? (function($){
上面的代码不是多余的吗?
最佳答案
As you know, this
$(document).ready(function(){
and this
(function($){
不,它们并不相同。他们有不同的目的。
当 html 文档准备好时,第一个获取处理程序。
(function(){...})();
会在脚本中一遇到就执行。
二是自执行功能。这不会等待文档准备就绪。
Isn't (function($){ redundant in code above?
我同意 (function($){
是多余的。没有必要。
关于javascript - 将 js 代码包装到 .ready() 两次合理吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43569446/