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