var obj = {}, textInput = document.querySelector('#textInput'),//获取input node节点对象 textSpan = document.querySelector('#textSpan');//获取span 节点对象 Object.defineProperty(obj, 'foo', { set: function (newValue) { textInput.value = newValue; textSpan.innerHTML = newValue; } }); textInput.addEventListener('keyup', function (e) { obj.foo = e.target.value;//赋值触发foo属性的set 方法,回调出新的值,这里的新值就是input元素中的vulue 值 });
<input type="text" id="textInput"> 输入:<span id="textSpan"></span>