<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>双向数据绑定</title>
</head>
<body>
<div id="all">
<input type="text" id="txt">
<p id="show-txt"></p>
</div>
<script>
var obj={};
var txt = document.getElementById("txt");
var show_txt = document.getElementById("show-txt");
Object.defineProperty(obj,‘txt‘,{
get : function () {
return obj;
},
set : function (value) {
show_txt.innerHTML = value;
}
});
txt.addEventListener(‘keyup‘,function (e) {
obj.txt = e.target.value;
});
</script>
</body>
</html>
原文地址:https://www.cnblogs.com/blogs-zong/p/12095887.html
时间: 2024-10-07 05:11:20