小编典典

检查是否使用Javascript加载了jquery

javascript

我正在尝试检查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>

阅读 315

收藏
2020-05-01

共1个答案

小编典典

事情不对

好吧,您正在使用jQuery检查jQuery是否存在。如果未加载jQuery,则根本$()不会运行,并且回调也不会执行,除非您正在使用另一个库,并且该库碰巧共享相同的$()语法。

删除您的$(document).ready()(使用类似方法window.onload):

window.onload = function() {
    if (window.jQuery) {  
        // jQuery is loaded  
        alert("Yeah!");
    } else {
        // jQuery is not loaded
        alert("Doesn't Work");
    }
}
2020-05-01