<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <input type="text"> <form action=""> <input type="text"> <input type="text"> <button type="reset">重置</button> <button type="submit">提交</button> </form> <script> var inputEle=document.querySelector('input') // 1.获取焦点和失去焦点 inputEle.addEventListener('focus',function(){ console.log('inputEle获取了焦点'); }) inputEle.addEventListener('blur',function(){ console.log('inputEle丢失了焦点'); }) // 2获取正在输入的内容和改变的内容 inputEle.addEventListener('input',function(){ console.log('正在输入'+inputEle.value); }) inputEle.addEventListener('change',function(){ console.log('改变输入值'+inputEle.value); }) // change和input的区别:change在丢失焦点时统一显示 // 3 监听重置和提交(在form中进行) var formEle=document.querySelector('form') formEle.addEventListener('reset',function(event){ console.log('进行了重置操作'); event.preventDefault() //阻止默认操作 }) formEle.addEventListener('submit',function(){ console.log('进行了提交操作'); }) </script> </body> </html>
标签:function,console,log,常见,表单,inputEle,事件,addEventListener,input From: https://www.cnblogs.com/theYT/p/17019630.html