我正在尝试编写一个程序来使用openweathermap.org API显示天气数据,同时使用MaterializeCSS使其看起来既漂亮又漂亮。但是,目前我的代码似乎在Electron中的加载与在我的Web浏览器中的加载不同。它可以在网络浏览器(最新版本的Google Chrome)中正常工作,而在Electron中,按钮和动画根本无法正常工作。没有动画,并且不同的选项卡无法正确显示。单击时,选项卡不执行任何操作,并且选项卡的所有内容立即显示。见下文。
https://i.stack.imgur.com/PdR4z.png
该文件正确显示在浏览器中,如下所示。
https://i.stack.imgur.com/kAYpS.png
这是index.html的代码:
const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')
const functions = require('./functions')
let win;
function createWindow () {
win = new BrowserWindow({width: 1280, height: 720});
win.loadURL('file://' + __dirname + '/index.html');
functions.getData(function(data) {
console.log(data);
});
//win.webContents.openDevTools()
win.on('closed', () => {
win = null
});
}
app.on('ready', createWindow);
app.on('window-all-closed', () => {
// mac stuff
if (process.platform !== 'darwin') {
app.quit()
}
});
app.on('activate', () => {
// more mac stuff
if (win === null) {
createWindow()
}
});
// materializecss.com for the the css used.
<!DOCTYPE html>
<html>
<head>
<!-- micron.js -->
<link href="https://unpkg.com/[email protected]/dist/css/micron.min.css" type="text/css" rel="stylesheet">
<!-- -->
<!-- materialize -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css">
<link rel="stylesheet" href="/css/style.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!-- -->
<!-- scripts -->
<script type="text/javascript" src="./functions.js"></script>
<!-- -->
<meta charset="utf-8">
<title>Material Weather</title>
</head>
<body>
<nav class="nav-extended light-blue">
<div class="nav-wrapper light-blue">
<a href="#" class="brand-logo center light-blue">Material Weather</a>
<a href="#" data-activates="mobile-demo" class="button-collapse"><i class="material-icons">menu</i></a>
</div>
<div class="nav-content light-blue">
<ul class="tabs tabs-fixed-width light-blue">
<li class="tab"><a href="#today" class="white-text">Today</a></li>
<li class="tab"><a href="#tomorrow" class="white-text">Tomorrow</a></li>
<li class="tab"><a href="#10day" class="white-text">10 Day</a></li>
</ul>
</div>
</nav>
<div id="today" class="col s12">Today</div>
<div id="tomorrow" class="col s12">Tomorrow</div>
<div id="10day" class="col s12">10 Day</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/script/micron.min.js" type="text/javascript"></script>
</body>
</html>
如果您需要任何其他信息,请随时询问。预先感谢您的宝贵时间。
最佳答案
问题是jQuery无法在Electron窗口中正确加载。在经过数小时的互联网搜索试图找到解决方法之后,这就是我想出的。
将其放在代码的脚本部分的开头:
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>
将其放在脚本部分的末尾:
<script>if (window.module) module = window.module;</script>
From what I've read,它实际上是强制窗口使用每个导入的模块,并且是解决其他类似问题的常用解决方法。
因此,我的代码现在看起来像这样:
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script> <!-- makes jquery actually work -->
<!-- scripts -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/script/micron.min.js" type="text/javascript"></script>
<!-- -->
<script>if (window.module) module = window.module;</script> <!-- makes jquery actually work -->```
关于javascript - HTML文件在Electron中的加载方式与在浏览器窗口中不同,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48633739/