Error: jQuery requires a window with a document

后端 未结 12 2324
说谎
说谎 2020-11-29 00:43

So everything was working just fine and great until doing npm update and now things do not work quite as they used to.

A little background: in my code I use jquery

12条回答
  •  醉梦人生
    2020-11-29 01:23

    I know that I am not in the correct year here, but I may have found a better way, then the ways that offered here. If you call the scripts from the .html file, the script will know the document, and it solved the problem for me (at least for now). example:

    index.html:

    
    
    
    
    
    
    
    
    
    
    
    
    
    

    initialize.js:

    var jQuery = require('jquery');
    
    (function($, global, undefined) {
        "use strict";
    
        var Initialize = function() {
            var initializer;          
            initializer = {
                materialize_init: function () {
                console.log(document.getElementsByClassName("tooltipped"));
                },
    
                main_initialize: function () {
                    this.materialize_init();
                }
            };
    
            return {
                main_init: function () {
                    initializer.main_initialize();
                    return this;
                }
            };
        };
    
        // AMD and window support
        if (typeof define === "function") {
            define([], function () { return new Initialize(); });
        } else if (typeof global.initializer === "undefined") {
            global.initializer = new Initialize();
        }
    
    })(jQuery, this);
    

    Let me know if it's wrong, I just started to work with this framework today.

提交回复
热议问题