如何在 JavaScript 中呼叫函式?


JavaScript 同樣允許我們編寫自己的函式。要稍後在指令碼中的另一個位置呼叫函式,你只需編寫該函式的名稱即可。

示例

你可以嘗試執行以下程式碼來了解如何在 JavaScript 中呼叫函式−

<html>
   <head>
      <script>
         function sayHello() {
            document.write ("Hello there!");
         }
      </script>
   </head>

   <body>
      <p>Click the following button to call the function</p>
      <form>
         <input type = "button" onclick = "sayHello()" value = "Say Hello">
      </form>
      <p>Use different text in write method and then try...</p>
   </body>
</html>

更新於:19-6 月-2020

933 次瀏覽

開啟你的 職業生涯

完成課程即可獲得認證

開始學習
廣告
© . All rights reserved.