<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text" v-model="content">
<input type="text" v-model="title">
<input type="text" v-model="title">
<h4 v-bind="content">这里也会发生更新</h4>
<h4 v-bind="title"></h4>
</body>
</html>
<script>
function View() {
let proxy = new Proxy(
{}, {
set(target,property,value) {
const ele=document.querySelectorAll(`[v-model=${property}]`);
ele.forEach((item)=>{
item.value=value;
})
const eleBind=document.querySelectorAll(`[v-bind=${property}]`);
eleBind.forEach((item)=>{
item.innerHTML=value;
})
},
get(target,property) {
}
}
)
this.init=function(){
const ele=document.querySelectorAll('input');
ele.forEach((item)=>{
item.addEventListener('keyup',function(){
proxy[this.getAttribute('v-model')]=this.value
})
})
}
}
new View().init()
</script>