我试图检查我的Jquery库是否加载到我的HTML页面。我正在检查它是否工作,但有些地方不太对劲。以下是我所拥有的:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script type="text/javascript" src="/query-1.6.3.min.js"></script>
        <script type="text/javascript">
          $(document).ready(function(){
             if (jQuery) {  
               // jQuery is loaded  
               alert("Yeah!");
             } else {
               // jQuery is not loaded
               alert("Doesn't Work");
             }
          });
        </script>

当前回答

if ('undefined' == typeof window.jQuery) {
    // jQuery not present
} else {
    // jQuery present
}

其他回答

按照这个链接:

if (typeof jQuery == 'undefined') {
    // jQuery IS NOT loaded, do stuff here.
}

there are a few more in comments of the link as well like,
if (typeof jQuery == 'function') {...}

//or

if (typeof $ == 'function') {...}

// or

if (jQuery) {
    console.log("jquery is loaded");
} else {
    console.log("Not loaded");
}

Hope this covers most of the good ways to get this thing done!!

只是一个可能真正解决问题的小修改:

window.onload = function() {
   if (window.jQuery) {  
       // jQuery is loaded  
       alert("Yeah!");
   } else {
    location.reload();
   }
}

而不是$(document). ready (function()使用窗口。Onload = function()。

一个快速的方法是在开发人员控制台中运行jQuery命令。在任何浏览器上按F12并尝试访问任何元素。

 $("#sideTab2").css("background-color", "yellow");

注意:不要用jQuery来测试你的jQuery(不是很明显)

使用jQuery

if ('undefined' == typeof window.jQuery) {
    $('#selector').appendTo('jQuery is NOT working');
} else {
    $('#selector').appendTo('jQuery is working');
}

使用javaScript

if ('undefined' == typeof window.jQuery) {
    document.getElementById('selector').innerHTML = "jQuery is NOT working";
} else {
    document.getElementById('selector').innerHTML = "jQuery is working";
}

如果jQuery是异步加载的,你可以等待它被定义,每一段时间检查它:

(function() {
    var a = setInterval( function() {
        if ( typeof window.jQuery === 'undefined' ) {
            return;
        }
        clearInterval( a );

        console.log( 'jQuery is loaded' ); // call your function with jQuery instead of this
    }, 500 );
})();

这个方法可以用于任何变量,你等待出现。