【问题标题】:Import JQuery in HTML with Electron使用 Electron 在 HTML 中导入 JQuery
【发布时间】:2019-12-26 16:59:08
【问题描述】:

我正在用 ElectronJS 编写一些 HTML 代码。我需要在 HTML 中使用 JQuery,所以我用npm install jquery 安装了 JQuery。

但是我应该导入什么文件来使用 JQuery?

<!DOCTYPE html>
<html lang="en">
    <head>
        <script src="??"></script>
        <script>
            $(document).keydown(function(event) {
                alert(event.which)
            })
        </script>
    </head>
    <body>
        foobar
    </body>
</html>

&lt;script src="??"&gt;我应该写什么?

(抱歉英语不好)

【问题讨论】:

标签: javascript jquery node.js npm electron


【解决方案1】:

只需在您的 html 中输入 &lt;script src="node_modules/jquery/dist/jquery.min.js"&gt;&lt;/script&gt;

注意:为了让你的脚本在 electron 上执行,你需要在你的 html 中编写如下内容:

  <script>
         if (typeof module === 'object') {
             window.module = module;
             module = undefined;
          }
   </script>

   <script src="node_modules/jquery/dist/jquery.min.js"></script>
    <script>
      $(document).keydown(function(event) {
                alert(event.which)
        })
   </script>
    //Put your other scripts here

     <script>
        if (window.module) module = window.module;

     </script>

【讨论】:

  • 我使用 npm install jquery --save 安装了 jQuery。出于某种原因,我需要使用完整的 Windows 路径。例如:/electron_apps/test_app/app1/node_modules/jquery/dist/jquery.min.js 相对路径,即。 node_modules/... 不起作用。
  • 尝试:手动下载 jQuery,然后放入您的项目文件夹。您可以编写存储 jQuery 文件的路径,而不是使用节点模块路径。例如,如果您将其存储在项目中的 assets 文件夹中: @PaulFlahr
猜你喜欢
  • 2021-02-26
  • 1970-01-01
  • 2020-08-07
  • 1970-01-01
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-17
相关资源
最近更新 更多