【问题标题】:openlayers and nodejs jquery not working offlineopenlayers 和 nodejs jquery 不能离线工作
【发布时间】:2019-10-23 03:53:20
【问题描述】:

我有一个可以在线运行 jquery 的 openlayers,但有时我无法访问互联网,我想离线使用它。我已经下载了lib但它不起作用,我确定它在“js”文件夹中,请帮助我

我正在使用 nodejs 和 openlayers

//working
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<link rel="stylesheet" href="css/bootstrap.min.css">   
<script src="js/bootstrap.min.js"></script>

//not working 
<script src="js/jquery-3.4.1.min.js"></script>    
<link rel="stylesheet" href="css/bootstrap.min.css">   
<script src="js/bootstrap.min.js"></script>

//browser error
Error: Bootstrap's JavaScript requires jQuery bootstrap.min.js:6:36
ReferenceError: $ is not defined

​它给了我这个错误,但使用在线 jquery 就可以了

【问题讨论】:

标签: javascript jquery html node.js openlayers


【解决方案1】:

首先你应该安装 jQuery。在您的项目文件夹中运行以下命令以安装 jQuery:

npm install jquery

你应该在 index.js 文件中导入它:

import {$,jQuery} from 'jquery';
// export for others scripts to use
window.$ = $;
window.jQuery = jQuery;

基于this 帖子。

或者你应该将它导入到你的 html 中:

<script src="node_modules/jquery/dist/jquery.min.js"></script>

如果您找到正确答案,请关闭其他社区(例如 gis.stackexchange.com)上的问题并参考答案。 谢谢!

希望对您有所帮助。

【讨论】:

  • 感谢您的回答,但这些解决方案都没有奏效。为什么使用在线 jquery 可以正常工作?很奇怪,但对我来说这不是解决方案,我需要它离线
  • 确定安装了吗?如果它已安装,您的 index.html 在哪里?您必须使用本地路径导入它。将您的 index.html 放在项目的根目录中(在 node_modules 旁边)。然后使用node_modules/jquery/dist/jquery.min.js 导入
【解决方案2】:

我刚刚用本地服务器解决了,不要问我为什么会这样,并且junt指向文件路径不起作用

首先,打开一个终端并进入 jquery.min.js 文件夹,然后启动一个简单的服务器(例如:python3 -m http.server 8080)

之后,转到索引。 html文件并添加源代码

<script src="http://localhost:8080/jquery-3.4.1.min.js"></script>

现在它可以离线工作了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多