繁体   English   中英

JavaScript & HTML: onclick button to run javascript function?

[英]JavaScript & HTML: onclick button to run javascript function?

我正在使用 electron 和 socket.io 设置 GUI 服务器应用程序。 我无法在index.html中获得 onclick 按钮来运行server.js中的startServer() function 。 我需要帮助

我尝试使用 ID 和事件侦听器来运行 function。 非常感谢您的帮助,谢谢。

主.js:

const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow

const path = require('path')

let mainWindow

function createWindow() {
  mainWindow = new BrowserWindow({
    height: 500,
    width: 700,
    frame: false,
    resizable: false,
    webPreferences: {
      nodeIntegration: true
    }
  });

  // Load html into window
  mainWindow.loadURL('file://' + __dirname + '/app/index.html');

  mainWindow.on('closed', () => {
    mainWindow = null
  })
}

// Listen for app to be ready
app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

索引.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <link rel="stylesheet" href="app.css">
</head>
<body>
  <script>server.js</script>
  <div class="disable-highlight">
    <h1>DNPS
    </h1>
  </div>
  <input type="button" onclick="startServer()" value="Start Server" />
  <script>
    require('./server.js')
  </script>
</body>
</html>

server.js:

const log = require('electron-log')

function startServer() {
  // Load Socket.io
  var app = require('express')();
  var http = require('http').createServer(app);
  var io = require('socket.io')(http);

  app.get('/', function(req, res){
    res.send('<h1>Hello world</h1>');
  });

  io.on('connection', function(socket){
    log.info('a user connected')
  });

  http.listen(3000, function(){
    log.info('listening on *:3000');
  });
};

经过多次摆弄后,我发现document.getElementById对我有用。

索引.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <link rel="stylesheet" href="app.css">
</head>
<body>
  <div class="disable-highlight">
    <h1>DNPS</h1>
  <input id="startBtn" type="button" value="Start Server"/>
  <script>require('./server.js')</script>
  <table class='center'>
    <td id="consoleLog">
    </td>
  </table>
  </div>
</body>
</html>

server.js:

const log = require('electron-log')

// Load Socket.io
const app = require('express')();
const http = require('http').createServer(app);
const io = require('socket.io')(http);

var tempLog = ''

function pushLog() {
  document.getElementById('consoleLog').innerHTML = tempLog
}

document.getElementById('startBtn').onclick = function() {
  app.get('/', function(req, res){
    res.sendFile(__dirname + '/output/index.html');
  });

  log.info('pushing index.html to localhost')
  tempLog += 'pushing index.html to localhost<br>'

  io.on('connection', function(socket){
    tempLog += 'a user connected<br>'
    log.info('a user connected<br>')
    pushLog()
  });

  http.listen(3000, function(){
    tempLog += 'listening on *:3000<br>'
    log.info('listening on *:3000');
    pushLog()
  });
};

暂无
暂无

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

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