[英]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中,这似乎是一个已知问题,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.