简体   繁体   English

为什么我不能在Electron中使用脚本标签引用jQuery?

[英]Why can't I reference jQuery using a script tag in Electron?

To use jquery inside nodejs electron app. 要在nodejs电子应用程序内使用jQuery。 i have to install jQuery via npm and use require('jquery') to reference jQuery . 我必须通过npm安装jQuery并使用require('jquery')引用jQuery

It works fine. 工作正常。

I need to know why i cant attach jquery like inside normal browser using script tags 我需要知道为什么我不能使用脚本标签像普通浏览器一样附加jQuery

<script src="jquery-1.12.1.min.js"></script> //getting error $ not defined

Code below using just script tags to attached jquery, its getting an error, $ is not defined 下面的代码仅使用脚本标签附加到jquery,它得到一个错误,未定义$

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="jquery-1.12.1.min.js"></script>
    <!--<script>-->
        <!--var $ = require('jquery');-->
    <!--</script>-->
    <script>
        $(document).ready(function(){});
    </script>
    <link href="style.css" rel="stylesheet">
</head>

<body>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum vel, ipsum nisi laboriosam nesciunt. Asperiores dolorum tempore quia, voluptatum laborum dolore officiis velit similique amet totam in? At, neque, assumenda.</p>
</body>

</html>

As already noted, jQuery tries to be smart and find out if it's in a browser or a node.js environment. 如前所述,jQuery试图变得聪明,并找出它是否在浏览器或node.js环境中。 In a browser it would define $ and jQuery but in a node.js environment it tries to export its main object by assigning it to module.exports . 在浏览器中,它将定义$jQuery但在node.js环境中,它将尝试通过将其分配给module.exports来导出其主要对象。

As the renderer process has node.js (and module ) available jQuery incorrectly assumes that it was included with require . 由于渲染器进程具有node.js(和module ),因此jQuery会错误地假定它已包含在require

So as a workaround you can use something like this: 因此,您可以使用以下解决方法:

<script src="path/to/jquery/jquery.js" type="text/javascript"></script>
<script type="text/javascript"> jQuery = $ = module.exports </script>

If you are trying that inside an electron app, then it is most likely the fact that the Jquery file is not in the path specified. 如果要在电子应用程序中进行尝试,则很可能是Jquery文件不在指定的路径中。 It would most likely be located in /node_modules/Jquery/... Something like that. 它很可能位于/ node_modules / Jquery / ...中。 Try an absolute path. 尝试绝对路径。 Alternatively, if the host computer is connected to the Internet you should be able to use the Jquery CDN. 或者,如果主机连接到Internet,则应该能够使用Jquery CDN。

This appears to be a known issue with a work around in Electron, 在Electron中,这似乎是一个已知问题,

Here #254 这里#254

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

相关问题 为什么我不能设置 Jquery 的属性并在 Electron 中得到一个未定义的答案 - Why can't I set the property of Jquery and get a undefined answer in Electron 为什么不能删除在Electron中使用fs-extra创建的目录? - Why can't I delete the directory that I've created using fs-extra in Electron? 如何通过以下方式包含jquery和datatables <script> tag in electron app - How to include jquery and datatables through <script> tag in electron app Electron:使用 showOpenDialog 允许在 Linux 和 Win10 上选择文件,但为什么我不能在 MacOS 上选择文件? - Electron : using showOpenDialog allows files to be chosen on Linux & Win10 but why can't I choose file on MacOS? 如何在 Electron 使用 jQuery? - How can I use jQuery at Electron? 我无法重新安装或更新Electron软件包 - I can't reinstall or update Electron package 我无法用npm更新电子 - I can't update electron with npm 在Electron中,为什么不能将节点模块中的值分配给angularjs视图? - In Electron, why can't i assign values from node modules to angularjs views? 我可以从JS中的JS引用Node JS函数吗<script> tag in a Jade template - Can I reference a Node JS function from JS within a <script> tag in a Jade template 尚未加载带有jQuery的Electron(electron-packager)脚本 - Electron (electron-packager) Script with jQuery is not loaded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM