首頁  >  文章  >  web前端  >  在HTML中,當元素接收到使用者輸入時執行腳本嗎?

在HTML中,當元素接收到使用者輸入時執行腳本嗎?

PHPz
PHPz轉載
2023-09-17 14:13:021208瀏覽

在HTML中,當元素接收到使用者輸入時執行腳本嗎?

使用oninput 事件屬性在元素取得使用者輸入時觸發。您可以嘗試執行以下程式碼來實作oninput 屬性 -

#範例

<!DOCTYPE html>
<html>
   <body>
      <p>Write your name below:</p>
      <input type = "text" id = "myid" oninput="display()">
      <p id="test"></p>
      <script>
         function display() {
            var p = document.getElementById("myid").value;
            document.getElementById("test").innerHTML = "Your answer is " + p;
         }
      </script>
   </body>
</html>

以上是在HTML中,當元素接收到使用者輸入時執行腳本嗎?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除