javascript/jquery中这两种格式的区别是什么?
鉴于这两个片段:javascript/jquery中这两种格式的区别是什么?,javascript,jquery,Javascript,Jquery,鉴于这两个片段: $.ready(function() { …… } 及 我似乎使用了第一种格式,但它不起作用,所以我想知道它们之间的区别是什么 另外,将放在页面底部和标题之间是否有区别?回答您的最后一个问题: 将javascript放在页面底部可以确保在执行之前加载页面的HTML。据我所知,在页面底部包含一个javascript文件会使其加载顺序排在所有其他文件之后 您提供的代码段在执行时应该是相同的,尽管使用第一个更合适。第二种感觉。。。多余。要回答您的最后一个问题: 将java
$.ready(function()
{
……
}
及
我似乎使用了第一种格式,但它不起作用,所以我想知道它们之间的区别是什么
另外,将
放在页面底部和标题之间是否有区别?回答您的最后一个问题:
将javascript放在页面底部可以确保在执行之前加载页面的HTML。据我所知,在页面底部包含一个javascript文件会使其加载顺序排在所有其他文件之后
您提供的代码段在执行时应该是相同的,尽管使用第一个更合适。第二种感觉。。。多余。要回答您的最后一个问题:
将javascript放在页面底部可以确保在执行之前加载页面的HTML。据我所知,在页面底部包含一个javascript文件会使其加载顺序排在所有其他文件之后
您提供的代码段在执行时应该是相同的,尽管使用第一个更合适。第二种感觉。。。冗余。两者之间的区别在于,第一个假设jQuery
设置为变量$
。第二种情况并非如此
第二个函数在沙盒中将$
定义为函数的参数,然后将该参数的jQuery
传入。即使在模式下,这也是使用速记的一种常见方式
(function($){/两者之间的区别在于,第一个假设jQuery
设置为变量$
,第二个则不设置
第二种方法将沙盒中的$
定义为函数的参数,然后为该参数传入jQuery
。即使在模式下,这也是使用速记$
的常用方法
(function($){/区别在于,这两个函数是不同的:
- 是用于触发就绪事件的内部方法。它不希望函数作为参数,而是布尔值
$(函数)
是的快捷方式,它是绑定就绪事件处理程序的API方法,即加载DOM时应运行的函数
所以,第一个“不起作用”,因为它根本不做你认为它能做的事。我建议你阅读
第二个包在中的事实对功能没有影响
另外,将
放在页面底部和页眉之间是否有区别
假设您正在谈论将处理程序绑定到就绪事件,那么不,关于执行没有区别
对于不想访问链接的用户:
jQuery.ready
:
// Handle when the DOM is ready
ready: function( wait ) {
// Abort if there are pending holds or we're already ready
if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) {
return;
}
// Remember that the DOM is ready
jQuery.isReady = true;
// If a normal DOM Ready event fired, decrement, and wait if need be
if ( wait !== true && --jQuery.readyWait > 0 ) {
return;
}
// If there are functions bound, to execute
readyList.resolveWith( document, [ jQuery ] );
// Trigger any bound ready events
if ( jQuery.fn.trigger ) {
jQuery( document ).trigger("ready").off("ready");
}
},
ready: function( fn ) {
// Add the callback
jQuery.ready.promise().done( fn );
return this;
},
jQuery.fn.ready
:
// Handle when the DOM is ready
ready: function( wait ) {
// Abort if there are pending holds or we're already ready
if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) {
return;
}
// Remember that the DOM is ready
jQuery.isReady = true;
// If a normal DOM Ready event fired, decrement, and wait if need be
if ( wait !== true && --jQuery.readyWait > 0 ) {
return;
}
// If there are functions bound, to execute
readyList.resolveWith( document, [ jQuery ] );
// Trigger any bound ready events
if ( jQuery.fn.trigger ) {
jQuery( document ).trigger("ready").off("ready");
}
},
ready: function( fn ) {
// Add the callback
jQuery.ready.promise().done( fn );
return this;
},
除其他外,区别在于这两种功能不同:
- 是用于触发就绪事件的内部方法。它不希望函数作为参数,而是布尔值
$(函数)
是的快捷方式,它是绑定就绪事件处理程序的API方法,即加载DOM时应运行的函数
所以,第一个“不起作用”,因为它根本不做你认为它能做的事。我建议你阅读
第二个包在中的事实对功能没有影响
另外,将
放在页面底部和页眉之间是否有区别
假设您正在谈论将处理程序绑定到就绪事件,那么不,关于执行没有区别
对于不想访问链接的用户:
jQuery.ready
:
// Handle when the DOM is ready
ready: function( wait ) {
// Abort if there are pending holds or we're already ready
if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) {
return;
}
// Remember that the DOM is ready
jQuery.isReady = true;
// If a normal DOM Ready event fired, decrement, and wait if need be
if ( wait !== true && --jQuery.readyWait > 0 ) {
return;
}
// If there are functions bound, to execute
readyList.resolveWith( document, [ jQuery ] );
// Trigger any bound ready events
if ( jQuery.fn.trigger ) {
jQuery( document ).trigger("ready").off("ready");
}
},
ready: function( fn ) {
// Add the callback
jQuery.ready.promise().done( fn );
return this;
},
jQuery.fn.ready
:
// Handle when the DOM is ready
ready: function( wait ) {
// Abort if there are pending holds or we're already ready
if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) {
return;
}
// Remember that the DOM is ready
jQuery.isReady = true;
// If a normal DOM Ready event fired, decrement, and wait if need be
if ( wait !== true && --jQuery.readyWait > 0 ) {
return;
}
// If there are functions bound, to execute
readyList.resolveWith( document, [ jQuery ] );
// Trigger any bound ready events
if ( jQuery.fn.trigger ) {
jQuery( document ).trigger("ready").off("ready");
}
},
ready: function( fn ) {
// Add the callback
jQuery.ready.promise().done( fn );
return this;
},
代码片段不完全相同。第一个调用jQuery.ready
,第二个调用jQuery.fn.ready
。代码片段不完全相同。第一个调用jQuery.ready
,第二个调用jQuery.fn.ready
。我个人觉得使用jQuery(函数($){…}更容易;
取而代之。我个人觉得使用jQuery(函数($){…})更容易;
取而代之。将脚本放在头部确实会影响加载行为(时间),而不是将它们放在
的末尾。将脚本放在头部确实会影响加载行为(时间)而不是将它们放在
的末尾。