javascript - 如何更改 Electron 中使用的 html 文件?
问题描述
我正在尝试更改 Electron 应用程序中使用的 html 文件。我在 StackOverFlow 上尝试了几个建议,但我无法实现任何目标。我的代码如下:
主.js:
<!DOCTYPE html>
<html>
<head>
<script src="renderer.js"></script>
</head>
<body>
<h1>One</h1>
<button id="change">Change</button>
</body>
</html>
新的.html:
<!DOCTYPE html>
<html>
<head></head>
<body>
<h1>Two</h1>
</body>
</html>
主.js:
const { app, BrowserWindow } = require('electron')
const path = require('path')
function createWindow () { // Create the browser window.
const mainWindow = new BrowserWindow({ width: 800, height: 600 })
mainWindow.loadFile('index.html')
mainWindow.openDevTools();
}
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
渲染器.js:
const {BrowserWindow} = require('electron').remote
const path = require('path')
const newWindowBtn = document.getElementById('change')
newWindowBtn.addEventListener('click', (event) => {
let win = new BrowserWindow({ width: 400, height: 320 })
win.on('close', () => { win = null })
win.loadFile("new.html");
})
一旦单击 index.html 中的按钮,我正在尝试将 html 文件更改为 new.html。
解决方案
您可以location.href='new.html'
在此功能中使用在一个窗口中更改页面。
推荐阅读
- ansible - Ansible - 当字符串不在变量中时触发失败
- python - CreateView 视力问题,django bug?
- java - 对于处于 ESTABLISHED 状态的 jedis sentinel,redis 服务器中打开的连接过多
- c# - C# Stream Reader 将阅读器转换为 Json
- java - 无法使用 Java 在 windows cmd 上显示 unicode 字符
- mysql - 如果某个字段在 MySQL 中更新,如何从相关表中删除?
- datagrip - DataGrip - 异常
- javascript - 在滚动时更改 div 比例
- c++ - 用一行代码替换字符串中所有句点的 C++ 方法?
- mysql - 每三个月自动编辑查询 MySQL 的 WHERE 条件