v-model (v2 原理)
<!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" placeholder="请输入名称" id="username"> 显示值:<p id="uName"></p> <script> let obj = {} Object.defineProperty(obj,'username',{ get: function(a){ console.log(a,"取之") }, set: function(b){ console.log(b,"shezhi") document.getElementById('uName').innerText = b; } }) document.getElementById("username").addEventListener('keyup',function(){ obj.username = event.target.value }) </script> </body> </html>