代码之家  ›  专栏  ›  技术社区  ›  Sunil

即使加载了jquery ui库,也未定义$.ui

  •  0
  • Sunil  · 技术社区  · 6 年前

    在下面的代码中,即使变量x和y正确地指示jQuery UI存在,但在 global.method1 函数变量z总是未定义的,而自jquery ui加载后就应该定义它。

    问题

    全局方法1

    我在下面的链接中有一个演示,其中我看不到这种行为,即在method1$.ui中定义了: sample runnable code . 所以,真的很困惑。

    var x = $.ui;//defined
    
    (function (global) {
    
         var dialogOptions;
    
        $(
            function () {
                var y = $.ui;//defined
    
                //some more code here
    
            }
        );
    
        global.method1 = function () {
            var z = $.ui; //not defined
    
           //some more code here
        }
    
        //some more code here
    
    })(window);
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Steve0    6 年前

    我试图为你树立一个最起码的榜样;

    • 向控制台添加了一些日志
    • 添加了对全局方法的调用

    一切似乎都已定义。您的问题必须在示例中未链接的代码中。

    var x = $.ui; //defined
    console.log(`first: ${x}`);
    (function(global) {
    
      var dialogOptions;
    
      $(
        function() {
          var y = $.ui; //defined
          console.log(`second: ${y}`);
          //some more code here
    
        }
      );
    
      global.method1 = function() {
        var z = $.ui; //not defined
        console.log(`third: ${z}`);
        //some more code here
      }
    
      //some more code here
      global.method1(); //added this call
    
    })(window);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>