在 HTML 中當元素被點選時執行指令碼?


在 HTML 中,使用 onclick 屬性可在單擊元素時執行指令碼。

示例

嘗試執行以下程式碼來實現 onclick 屬性 -

<!DOCTYPE html>
<html>
   <body>
      <button onclick = "display()">Click</button>

      <p id = "test"></p>

      <script>
         function display() {
            document.getElementById("test").innerHTML = "You clicked the button!";
         }
      </script>
   </body>
</html>

更新於: 2020 年 3 月 3 日

2K+ 檢視次數

開啟你的事業

完成課程認證

開始
廣告