简单js双向数据绑定
html部分
<div id
="app">
<input type
="text" id
="txt">
<p id
="show"></p
>
</div
>
js部分
<script
>
var obj
= {}
Object
.defineProperty(obj
, 'txt', {
get: function() {
return obj
},
set: function(newValue
) {
document
.getElementById('txt').value
= newValue
document
.getElementById('show').innerHTML
= newValue
}
})
document
.addEventListener('keyup', function(e
) {
obj
.txt
= e
.target
.value
})
</script
>
转载请注明原文地址:https://ipadbbs.8miu.com/read-56308.html