1 回答
TA贡献1784条经验 获得超2个赞
用defineProperty吧
<input type="text" id="aaa" value="1" />
<button id="btn">add1</button>
var aaa =document.getElementById("aaa");
var btn =document.getElementById("btn");
var obj = {};
btn.onclick=function(){
if(aaa.value){
obj.val = parseInt(aaa.value)+1;
}else{
obj.val = 1;
}
}
valcahnge(aaa,function(){
console.log(aaa.value);
});
function valcahnge(dom,fn){
Object.defineProperty(obj,'val',{
get:function(){
return this._val;
},
set:function(val){
dom.value = val;
fn();
this._value=val;
}
});
}
添加回答
举报