如何單擊呼叫 JavaScript 函式?


如要在 JavaScript 中單擊某個按鈕時呼叫某個函式,請使用 onclick。嘗試執行以下程式碼以在 JavaScript 中單擊 onclick 按鈕呼叫一個函式 −

示例

<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

1 千+ 次瀏覽

開啟你的職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.