<!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>
<div>
请输入聊天内容: <input type="text" id="myInput" />
<h1 class="msg"></h1>
</div>
<script>
let _input = document.querySelector("#myInput");
let _msg = document.querySelector('.msg');
console.log(_msg);
var obj = {};
Object.defineProperty(obj, 'value', {
get: function () {
return "";
},
set: function (v) {
_msg.innerHTML = v
}
})
_input.addEventListener("input",function(e){
obj.value = e.target.value;
})
</script>
</body>
</html>
运行: