简体   繁体   English

操作 jQuery 得到一个错误:“jQuery 需要一个窗口和一个文档”

[英]Manipulating jQuery to get an error: "jQuery requires a window and a document"

If anyone flicked through the uncompressed version of jQuery, one may encounter something like:如果有人浏览了 jQuery 的未压缩版本,可能会遇到以下情况:

if ( typeof module === "object" && typeof module.exports === "object" ) {

        // For CommonJS and CommonJS-like environments where a proper `window`
        // is present, execute the factory and get jQuery.
        // For environments that do not have a `window` with a `document`
        // (such as Node.js), expose a factory as module.exports.
        // This accentuates the need for the creation of a real `window`.
        // e.g. var jQuery = require("jquery")(window);
        // See ticket #14549 for more info.
        module.exports = global.document ?
            factory( global, true ) :
            function( w ) {
                if ( !w.document ) {
                    throw new Error( "jQuery requires a window with a document" );
                }
                return factory( w );
            };
    } else {
        factory( global );
    }

Notice the throw new Error() part.注意throw new Error()部分。 If I am not mistaken, it only throws the error if a window and document are not present.如果我没记错的话,它只会在不存在窗口和文档时抛出错误。 I am aware that things like Node.js could trigger this error, but can a normal HTML, CSS and JS web page trigger this in any way?我知道像 Node.js 这样的东西可能会触发这个错误,但是普通的 HTML、CSS 和 JS 网页可以以任何方式触发这个错误吗?

  1. Node.js will not throw an error here since it contains a global object with a "document" inside it; Node.js 不会在这里抛出错误,因为它包含一个全局对象,其中包含一个“文档”; you can use jquery with Node.js.你可以在 Node.js 中使用 jquery。

  2. You'd be better off using CherrioJS , which is designed specifically to be "like jQuery but for the server.您最好使用CherrioJS ,它专门设计为“类似于 jQuery,但用于服务器。

  3. Attempts to use jQuery with " ElectronJS " or " Headless Chrome " are examples where this error will throw.尝试将 jQuery 与“ ElectronJS ”或“ Headless Chrome ”一起使用是会抛出此错误的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “ jQuery需要带有文档的窗口” Webpack 2错误 - “jQuery requires a window with a document” webpack 2 error Browserify with jQuery> = 2产生“jQuery需要一个带文档的窗口” - Browserify with jQuery >= 2 produces “jQuery requires a window with a document” 如何使用导入解决“ jQuery需要带有文档的窗口” - How to resolve “jQuery requires a window with a document” with import 错误:jQuery需要一个window带证件。 使用ejs和javascript创建刽子手web app - Error : jQuery requires a window with a document. Using ejs and javascript to create a hangman web app 使用节点jquery处理现有文档 - Manipulating an existing document using node jquery jQuery get()需要2次通过? - Jquery get() requires 2 passes? 茉莉花-错误:茉莉花固定装置需要在window.jQuery或window。$中定义jQuery。 - Jasmine - Error: jasmine-fixture requires jQuery to be defined at window.jQuery or window.$ 使用jquery从父窗口遍历/操作新窗口 - Traversing/Manipulating new window from parent window with jquery 引导JavaScript需要jQuery错误 - bootstrap javascript requires jquery error $(window).on('load') 在 jQuery(document).ready() 中没有被正确调用 - $(window).on('load') does not get called correctly inside jQuery(document).ready()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM