簡體   English   中英

如何將Javascript函數的值顯示在HTML頁面上?

[英]How to display value from a Javascript function onto an HTML page?

index.js文件

const {BrowserWindow, app, globalShortcut} = require('electron');
const url = require('url');
const si = require('systeminformation');



let win = null

function boot() {
//console.log(process.type)
win = new BrowserWindow({
    width: 600,
    height: 500,
    frame: false
})

var output = document.getElementById("output");
output.innerHTML = "hello world"
//win.loadURL(file:'//${__dirname}/index.html')
win.loadURL('file://' + __dirname + '/index.html');
win.on('closed', () => {
    win = null
})
}

app.on('ready', boot); 

** index.html文件**

<!DOCTYPE html>
<html lang= "en">
<head>
<meta charset="UTF-8">
<title>Hello World</title>
<link rel="stylesheet" href="style.css">

</head>
<body>
<div id="content">
    <header>
        <div class="option" id="close">X</div>
        <div class="option" id="minimize">-</div>
    </header>
<div id="text">z</div>


 <h1>
  <p id="output"></p>
</h1>
</div>
<script src="./assets/js/script.js"></script>
</body>
</html>

所以我正在使用以下代碼片段中的值顯示在我的HTML頁面中

var output = document.getElementById("output");
output.innerHTML = "hello world"

通過此操作在我的HTML頁面上:

<h1>
  <p id="output"></p>
</h1>

但這給了我錯誤:

“引用錯誤:未定義文檔”

順便說一句,我正在創建一個電子應用程序。 只是試圖顯示一些數據從我的javascript頁面到html頁面。

根據您提供的代碼,您在加載文檔之前先參考該文檔。

var output = document.getElementById("output"); // <- here and
output.innerHTML = "hello world";
win.loadURL('file://' + __dirname + '/index.html'); // <- here

在操作之前,請檢查DOM是否准備就緒。

您的JavaScript文件的名稱是index.js。 但是您要在script標簽中包含script.js文件。 更改腳本標記以指定index.js文件的路徑:

<script src="./path_of_file/index.js"></script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM