原生手写双向绑定

博客围绕原生双向绑定展开,但具体内容缺失。双向绑定是前端开发重要概念,能实现数据与视图自动同步,提高开发效率和用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		数量<input type="text" id="num">
		价格<input type="text" id="price">
		总价<div id="total">
	</body>
	<script type="text/javascript">
		var num = document.querySelector("#num")
		var price = document.querySelector("#price")
		var total = document.querySelector("#total")
		
		let shopMsg = {
			num:10,
			price:100,
			set _num(val){
				this.num = val;
				total.innerHTML = this.num * this.price;
			},
			set _price(val){
				this.price = val;
				total.innerHTML = this.price * this.num;
			}
		}
		num.value = shopMsg.num;
		price.value = shopMsg.price;
		total.innerHTML = shopMsg.num*shopMsg.price;
		
		num.oninput = function(){
			shopMsg["_num"] = this.value;
		}
		price.oninput = function(e){
			shopMsg["_price"] = this.value;
		}
		
	</script>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值