【发布时间】:2015-12-23 04:48:30
【问题描述】:
我正在尝试向index.html 文件中的按钮添加功能,如下所示:
我在index.html中有一个按钮元素
<button id="auth-button">Authorize</button>
在应用程序的main.js,我有
require('crash-reporter').start();
console.log("oh yaeh!");
var mainWindow = null;
app.on('window-all-closed', function(){
if(process.platform != 'darwin'){
app.quit();
}
});
app.on('ready',function(){
mainWindow = new BrowserWindow({width:800, height : 600});
mainWindow.loadUrl('file://' + __dirname + '/index.html');
var authButton = document.getElementById("auth-button");
authButton.addEventListener("click",function(){alert("clicked!");});
mainWindow.openDevTools();
mainWindow.on('closed',function(){
mainWindow = null;
});
});
但是出现如下错误:
Uncaught Exception: ReferenceError: document is not defined
在构建电子应用程序时可以访问 DOM 对象吗?还是有任何其他替代方式可以为我提供所需的功能?
【问题讨论】:
-
主进程无权访问 DOM,只有渲染器才能访问。 Learn the difference
-
你能把你的 index.html 贴在这里
标签: javascript dom electron