如何检查客户端机器上加载了jQuery的哪个版本?客户端可能有jQuery加载,但我不知道如何检查它。如果他们有加载,我如何检查版本和前缀,如:
$('.class')
JQuery('.class')
如何检查客户端机器上加载了jQuery的哪个版本?客户端可能有jQuery加载,但我不知道如何检查它。如果他们有加载,我如何检查版本和前缀,如:
$('.class')
JQuery('.class')
当前回答
也许不言自明,但我发现的最简单的方法是查看实际文件中注释的版本(通常是jquery.min.js)。或者,如果使用类似code.jquery.com的东西,则版本号在https引用中(即https://code.jquery.com/jquery-1.11.3.js)。
在这里输入图像描述
其他回答
你可以检查jQuery对象是否存在:
if( typeof jQuery !== 'undefined' ) ... // jQuery loaded
jQuery()。Jquery有版本号。
至于前缀,jQuery应该总是有效的。如果你想使用$,你可以将你的代码包装到一个函数中,并将jQuery作为参数传递给它:
(function( $ ) {
$( '.class' ).doSomething(); // works always
})( jQuery )
你应该在IE的try/catch块中封装这个:
// Ensure jquery is loaded -- syntaxed for IE compatibility
try
{
var jqueryIsLoaded=jQuery;
jQueryIsLoaded=true;
}
catch(err)
{
var jQueryIsLoaded=false;
}
if(jQueryIsLoaded)
{
$(function(){
/** site level jquery code here **/
});
}
else
{
// Jquery not loaded
}
在一行和最少的按键(哎呀!):
alert($().jquery);
转到开发人员工具>控制台,并编写以下命令之一 jQuery.fn.jquery console.log (jQuery () .jquery);
if (typeof jQuery != 'undefined') {
// jQuery is loaded => print the version
alert(jQuery.fn.jquery);
}