在 HTML 中,当元素获得用户输入时执行脚本?

htmlweb developmentfront end technology更新于 2025/8/15 2:20:17

使用 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>

相关文章


有用资源