一、双向绑定
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="text" v-model="userName"><br/>
<input type="text" v-model="userName"><br/>
<input type="text" v-model="userName">
</body>
<script>
function setModel(name){
var vModels=document.querySelectorAll('[v-model="'+name+ '"]');
Object.defineProperty(window,name,{
get:function(){
return vModels[0].value;
},
set:function(newValue){
vModels.forEach(function(model){
model.value=newValue;
});
}
});
vModels.forEach(function(model){
model.addEventListener('input',function(e){
window[name] = e.target.value;
});
});
}
setModel('userName');
</script>
</html>
二、数据监听
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="app">
<input type="text" id="txt">
<p id="show"></p>
</div>
</body>
<script type="text/javascript">
var obj={}
document.addEventListener('keyup',function(e){
obj.txt=e.target.value
})
Object.defineProperty(obj,'txt',{
get:function(){
return obj
},
set:function(newValue){
document.getElementById('txt').value=newValue
document.getElementById('show').innerHTML=newValue
}
})
</script>
</html>
</html>