簡體   English   中英

操作 jQuery 得到一個錯誤:“jQuery 需要一個窗口和一個文檔”

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

如果有人瀏覽了 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 );
    }

注意throw new Error()部分。 如果我沒記錯的話,它只會在不存在窗口和文檔時拋出錯誤。 我知道像 Node.js 這樣的東西可能會觸發這個錯誤,但是普通的 HTML、CSS 和 JS 網頁可以以任何方式觸發這個錯誤嗎?

  1. Node.js 不會在這里拋出錯誤,因為它包含一個全局對象,其中包含一個“文檔”; 你可以在 Node.js 中使用 jquery。

  2. 您最好使用CherrioJS ,它專門設計為“類似於 jQuery,但用於服務器。

  3. 嘗試將 jQuery 與“ ElectronJS ”或“ Headless Chrome ”一起使用是會拋出此錯誤的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM