簡體   English   中英

Javascript:如何從腳本文件中的html頁面調用函數

[英]Javascript : how to call a function from an html page that is in a script file

我有兩個文件,一個名為index.html ,另一個名為index.js我將Node.jsElectron結合使用來制造一個不和諧的機器人。 由於電子原因,我的界面是html格式,因此我有一些文本框可以更新我的機器人的狀態。 我使用Discord.js

這是導致出現問題index.html的代碼:

<div id="ActivityChanger">
        <select id="ActivityTypeList">
          <option value="none">None</option>
          <option value="PLAYING">Playing</option>
          <option value="WATCHING">Watching</option>
          <option value="LISTENING">Listening To</option>
          <option value="STREAMING">Streaming</option>
        </select>
        <input type="text" name="TEST" size="40" id="ActivityGame" />
        <button name="Update" type="submit" value="update-true" id="UpdateButton" onclick="UpdateActivity()">
        Update
        </button>
        <script scr="index.js"></script>
      </div>

按下“ Update按鈕時,此代碼通常會發送文本框和下拉列表的信息,但我不想在.html文件中包含Javascript,因此可以使用

function UpdateActivity(){
    var selectedValue = document.getElementById("ActivityTypeList").value;
    var selectedGame = document.getElementById("ActivityGame").value;
    if((selectedValue !== 'none') && (selectedGame !== null)){
      UserBot.user.setActivity(selectedGame, {type: selectedValue,})
      console.log('Updated Status' + ` to ${selectedValue} ${selectedGame} `)
    }
}

UserBot是我的客戶

但是當我在控制台中按下burron時,它說未定義UpdateActivity()

如果這是您包含的真實HTML,則其中包含一個錯字:

<script scr="index.js"></script>

那應該是src ,而不是scr

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

暫無
暫無

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

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