简单实现VUE的双向数据绑定

<!DOCTYPE html>
<html>
<head>
	<title>vue-双向数据绑定的简单实现</title>
</head>
<body>
	<input type="text" name="" id="inputText">
	<span id='textSpan'></span>
	<script type="text/javascript">
	let obj = {},
		inputText = document.querySelector('#inputText'),
		textSpan = document.querySelector('#textSpan')
	Object.defineProperty(obj, 'foo', {
		set: function (newValue) {
			inputText.value = newValue
			textSpan.innerHTML = newValue
		}
	})
	inputText.addEventListener('keyup', function (e) {
		obj.foo = e.target.value
	})
	</script>
</body>
</html>

  

猜你喜欢

转载自www.cnblogs.com/maggie-pan/p/10025803.html