首页 > 解决方案 > 使用 Electron 在 HTML 中导入 JQuery

问题描述

我正在用 ElectronJS 编写一些 HTML 代码。我需要在 HTML 中使用 JQuery,所以我用npm install 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>

<script src="??">应该写什么?

(抱歉英语不好)

标签: javascriptjquerynode.jsnpmelectron

解决方案


只需<script src="node_modules/jquery/dist/jquery.min.js"></script>输入您的html。

注意:为了让您的脚本在电子上执行,您需要在 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>

推荐阅读