简体   繁体   English

电子 HTML Javascript 按钮不起作用

[英]Electron HTML Javascript button not working

I'm trying to create and save files using javascript.我正在尝试使用 javascript 创建和保存文件。
This is my HTML code:这是我的 HTML 代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Tutorial</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.con/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="./css/styles.css"> 
</head>
<body>
<div class="container" id="form-container">
<textarea class= "form-control" placeholder ="File Contents..." rows="10"></textarea>
<input id= "content" class="form-control" placeholder="Input Text Here...">
<button id="createButton" class="btn btn-success">Create File</button>
</div>
</body>

<script>
    require('./renderer.js')
</script>
</html>  

创建文件按钮不起作用 The page looks like this currently.该页面目前看起来像这样。 For some reason I don't understand, the button to create file doesn't work I click it but nothing happens.出于某种原因我不明白,创建文件的按钮不起作用我点击它但没有任何反应。 It does not create a file, nor does it open a location where you want to save the file.它不会创建文件,也不会打开要保存文件的位置。

This is my Javascript code:这是我的 Javascript 代码:

var app = require('electron').remote;
var dialog = app.dialog;
var fs = require('fs');

document.getElementbyId('createButton').onclick = () => {
    dialog.showSaveDialog((fileName) => {
        if(fileName === undefined){
            alert("You didnt save the file");
            return;
        }

        var content = document.getElementbyId('content').value;
        fs.writeFile(FileName, content, (err) => {
            if(err) console.log(err);
            alert("The file has been succesfully saved")
        })
    });
};

Why won't the 'Create File' button work?为什么“创建文件”按钮不起作用?

The function you are trying to use is called document.getElementById , using the camel-case naming convention.您尝试使用的函数称为document.getElementById ,使用驼峰命名约定。 Your code would look like this, when corrected:更正后,您的代码将如下所示:

//... require statements as in your code
document.getElementById("createButton").onclick = () => {
    // ...
    var content = document.getElementById("content").value;
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM