Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查加载了哪个版本的jQuery?_Jquery - Fatal编程技术网

如何检查加载了哪个版本的jQuery?

如何检查加载了哪个版本的jQuery?,jquery,Jquery,如何检查客户端计算机上加载了哪个版本的jQuery?客户端可能加载了jQuery,但我不知道如何检查它。如果已加载,如何检查版本和前缀,例如: $('.class') JQuery('.class') 您只需检查jQuery对象是否存在: if( typeof jQuery !== 'undefined' ) ... // jQuery loaded jQuery()。jQuery具有版本号 至于前缀,jQuery应该始终有效。如果要使用$,可以将代码包装到函数中,并将jQuery作为参数传

如何检查客户端计算机上加载了哪个版本的jQuery?客户端可能加载了jQuery,但我不知道如何检查它。如果已加载,如何检查版本和前缀,例如:

$('.class')
JQuery('.class')

您只需检查
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
}

我发现这是检查jQuery是否已加载的最短、最简单的方法:

if (window.jQuery) {
    // jQuery is available.

    // Print the jQuery version, e.g. "1.0.0":
    console.log(window.jQuery.fn.jquery);
}

此方法由和其他人使用。

…仅因为此方法尚未提及-打开控制台并键入:

$ === jQuery
正如上面提到的@Juhana
$()。jquery
将返回版本号。

我的首选是:

console.debug("jQuery "+ (jQuery ? $().jquery : "NOT") +" loaded")
结果:

jQuery 1.8.0已加载

// My original 'goto' means to get the version
$.fn.jquery

// Another *similar* option
$().jQuery

// If there is concern that there may be multiple implementations of `$` then:
jQuery.fn.jquery
最近,我在一些网站上使用
$.fn.jquery
/
$().jquery
时遇到问题,所以我想注意第三个简单的命令来获取jquery版本

如果您得到一个版本号(通常是字符串),那么jQuery将被加载,这就是您正在使用的版本。如果未加载,则应返回未定义的
,甚至可能返回错误

这是一个很老的问题,我已经看到一些人在评论中提到了我的答案。然而,我发现有时作为评论留下的好答案可能会被忽略;特别是当一个答案有很多评论时,你可能会发现自己在成堆的评论中寻找宝石。希望这能帮到别人

根据,键入,下面这些脚本将为您提供当前正在遍历的站点中的jquery版本

 1. console.log(jQuery.fn.jquery);
 2. console.log(jQuery().jquery);

转到开发人员的工具>控制台并编写以下命令之一
jQuery.fn.jQuery

console.log(jQuery().jQuery)

在一行中,并且击键次数最少(oops!):


也许是不言自明的,但我发现最简单的方法是查看实际文件(通常是jquery.min.js)中注释的版本。或者,如果使用类似code.jquery.com的内容,则版本号在https参考(ie)中



如果您将jQuery作为要下载的文件包含在内,则客户端可能已加载jQuery。到底是什么问题?不知道OP需要它做什么,但是如果你正在制作一个需要jQuery的脚本或插件,如果它不存在或者版本太旧,你可以检查并抛出一个错误。
if(jQuery)可能重复如果
jQuery
未知且不起作用,则
将引发异常。可以将其包装在
中,尝试{…}catch(e){…}
?不要使用
jQuery().jQuery
。它会创建一个jQuery对象。(从另一个答案中复制评论)其他一些库使用
$
。如果他们使用的是Prototype呢?或者使用
$
的任何其他内容?它不是jQuery独有的。如果没有加载
jQuery
,该怎么办?(它将抛出一个
ReferenceError
)。上面的答案是正确的,这就是为什么人们对你的答案投反对票。IE不处理jQuery的
typeof!='未定义“
?这对每一款IE来说都是真的吗?还是只对老款IE来说都是真的?很确定这是对旧版的。还有什么比一个大的ol'JS错误阻止页面工作更令人尴尬的呢,不管这是不是浏览器的错误;)这真的帮了我很大的忙。我一直在努力了解Firefox和IE的确切行为,以检查jQuery是否使用WatiN加载。现在有了这个解决办法,我很高兴。谢谢你!有两种方法可以检查当前加载的jquery版本:
jquery.fn.jquery
jquery().jquery
(缩写:
$.fn.jquery
$().jquery
)。如果你想更深入地挖掘,我写了关于jquery源代码的链接。更简单的版本
if(window.jquery){//jquery已加载=>打印版本警报(jquery.fn.jquery);}
未定义的检查应该是
==注意小写:)不要使用
jQuery().jQuery
。它会徒劳地创建一个jQuery对象,只需使用答案所说的内容即可。@AndreFigueiredo在确保jQuery已加载后,我编辑了答案以包括如何获取版本号。@Janar不,它一点都不相同。相比之下,我觉得太复杂了。您在控制台中尝试过键入“5”吗?然后下一个“$”将给您“5”。类似于
($==jQuery)?$()OP要求检查加载的版本。这并不能回答问题。我明白了:undefinedThis更好一点,但在使用应用程序时,日志记录并没有那么有用。最好先保存一个变量<代码>让具有$=typeof jQuery=='function';log('jQuery'+(已加载$?`version${jQuery.fn.jQuery}。`:'未加载')
// My original 'goto' means to get the version
$.fn.jquery

// Another *similar* option
$().jQuery

// If there is concern that there may be multiple implementations of `$` then:
jQuery.fn.jquery
 1. console.log(jQuery.fn.jquery);
 2. console.log(jQuery().jquery);
alert($().jquery);