本文我们将和大家分享前端之js双向数据绑定,希望对大家有帮助。
<html>
<head>
<title>textBind</title>
</head>
<body>
<input type="text" name="infoInsert">
<p id="infoShow"></p>
<script type="text/javascript">
var obj = {
seeYou: 'Hello'
};
Object.defineProperty(obj, 'infoBind', {
get: function () {
return this.seeYou;
},
set: function (newValue) {
document.getElementById('infoShow').innerText = newValue;
document.getElementsByName('infoInsert')[0].value = newValue;
}
});
document.getElementsByName('infoInsert')[0].addEventListener('keyup', function () {
obj.infoBind = this.value;
});
</script>
</body>
</html>通过以上代码,不难看出框架中使用的双向数据绑定的底层实现原理是通过ES5中的defineProperty属性来实现的。
相关推荐:
立即学习“前端免费学习笔记(深入)”;
以上就是前端之js双向数据绑定的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号