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

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

当前回答

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

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

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

http://html5boilerplate.com和其他公司使用了这种方法。

其他回答

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

alert($().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
}

使用可选的链接操作符(?.),这可以用window.jQuery?.fn?.jquery完成。如果根本没有加载jQuery,这一行也可以工作。

if (jQuery){
   //jquery loaded
}

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

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

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

http://html5boilerplate.com和其他公司使用了这种方法。