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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6973941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:38:35  来源:igfitidea点击:

How to check what version of jQuery is loaded?

jquery

提问by Luke101

How do I check which version of jQuery is loaded on the client machine? The client may have jQuery loaded but I don't know how to check it. If they have it loaded how do I check the version and the prefix such as:

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

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

回答by Darin Dimitrov

if (typeof jQuery != 'undefined') {  
    // jQuery is loaded => print the version
    alert(jQuery.fn.jquery);
}

回答by JJJ

You can just check if the jQueryobject exists:

您可以检查jQuery对象是否存在:

if( typeof jQuery !== 'undefined' ) ... // jQuery loaded

jQuery().jqueryhas the version number.

jQuery().jquery有版本号。

As for the prefix, jQueryshould always work. If you want to use $you can wrap your code to a function and pass jQueryto it as the parameter:

至于前缀,jQuery应该总是有效的。如果你想使用$你可以将你的代码包装成一个函数并jQuery作为参数传递给它:

(function( $ ) {
    $( '.class' ).doSomething();  // works always
})( jQuery )

回答by Rockin4Life33

$.fn.jquery
// If there is concern that there may be multiple implementations of `$` then:
jQuery.fn.jquery

If you get back a version number -- usually as a string -- then jQuery is loaded and that is what version you're working with. If not loaded then you should get back undefinedor maybe even an error.

如果你得到一个版本号——通常是一个字符串——那么 jQuery 就会被加载,这就是你正在使用的版本。如果没有加载,那么你应该返回undefined或者甚至是一个错误。

Pretty old question and I've seen a few people that have already mentioned my answer in comments. However, I find that sometimes great answers that are left as comments can go unnoticed; especially when there are a lot of comments to an answer you may find yourself digging through piles of them looking for a gem. Hopefully this helps someone out!

很老的问题,我看到一些人已经在评论中提到了我的答案。但是,我发现有时作为评论留下的好答案可能会被忽视;尤其是当一个答案有很多评论时,您可能会发现自己在成堆的评论中寻找宝石。希望这可以帮助某人!

回答by ptim

…just because this method hasn't been mentioned so far - open the console and type:

...只是因为到目前为止还没有提到这个方法 - 打开控制台并输入:

$ === jQuery

As @Juhana mentioned above $().jquerywill return the version number.

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

回答by Markus Amalthea Magnuson

I have found this to be the shortest and simplest way to check if jQuery is loaded:

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

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

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

This method is used by http://html5boilerplate.comand others.

这个方法被http://html5boilerplate.com和其他人使用。

回答by sshel207

My preference is:

我的偏好是:

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

Result:

结果:

jQuery 1.8.0 loaded

jQuery 1.8.0 加载

回答by AlienWebguy

You should actually wrap this in a try/catch block for IE:

您实际上应该将其包装在 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
}

回答by David White

In one line and the minimum of keystrokes (oops!):

在一行和最少的击键中(哎呀!):

alert($().jquery);

回答by Suman Ktheitroadala

Go to the developer's Tool > console and write one of the following command jQuery.fn.jqueryconsole.log(jQuery().jquery);

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

回答by aniruddha

As per Template monster blog, typing, these below scripts will give you the version of the jquery in the site you are traversing now.

根据模板怪物博客,键入以下脚本将为您提供您现在正在遍历的站点中的 jquery 版本。

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