当前位置:网站首页>JS to realize bidirectional data binding
JS to realize bidirectional data binding
2022-06-13 06:13:00 【A strong foam】
Implement a two-way data binding ,input While the value changes ,<span> Label synchronization input The content of .
<div>
<input type="text" id="input_1">
<span id="span_1"></span>
</div>
<script type="text/javascript">
var obj = {
};
Object.defineProperty(obj, 'test', {
set: (newVal)=>{
//document.getElementById('input_1').value = newVal;
document.getElementById('span_1').innerHTML = newVal;
},
// There is no need for get Method
get: ()=>{
alert(' The data was read ')
}
});
document.addEventListener('keyup', (e)=>{
obj.test = e.target.value; // Event monitoring ,input The input field changes , be obj.test attribute ( Equivalent to an intermediary ) It will change , Then change span Content of the label
})
</script>
Screenshot :
Reference blog : Reference resources 1、 Reference resources 2
边栏推荐
- [written examination questions of meituan]
- CORS request principle
- Leetcode- divide candy - simple
- Self summarizing
- Essays on November 5, 2021
- Uniapp mobile terminal uses canvas to draw background convex arc
- [MySQL] basic knowledge review
- Hbuilderx: installation of hbuilderx and its common plug-ins
- Adding classes dynamically in uni app
- Echart柱状图:x轴显示value,y轴显示类别
猜你喜欢

Status management --provider

Use of Nacos configuration center

Detailed explanation of PHP distributed transaction principle
![[turn] explain awk (1)__ Awk Basics_ Options_ Program segment parsing and examples](/img/65/a214d137e230b1a1190feb03660f2c.jpg)
[turn] explain awk (1)__ Awk Basics_ Options_ Program segment parsing and examples

Free screen recording software captura download and installation

Echart rectangular tree diagram: simple implementation of rectangular tree diagram

Echart柱状图:堆叠柱状图显示value

Huawei developer certification and deveco studio compiler Download

本地文件秒搜工具 Everything

【ONE·Data || 带头双向循环链表简单实现】
随机推荐
微信小程序:基础复习
Wechat applet (function transfer parameters, transfer multiple parameters, page Jump)
[compilation and assembly link] coff file and structure description
PHP redis makes high burst spike
USB 0xc0000011 error
Applet Use of spaces
Echart折线图:多条折线图每次仅展示一条
Echart histogram: stack histogram value formatted display
Echart矩形树图:简单实现矩形树图
[DP 01 backpack]
Uniapp dynamically shows / hides the navigation bar return button
FusionPBX 安装 —— 筑梦之路
php redis 制作高迸发秒杀
You should consider upgrading via
Echart折线图:当多条折线图的name一样时也显示不同的颜色
Huawei developer certification and deveco studio compiler Download
Leetcode- hex number - simple
自定义View —— 可伸展的CollapsExpendView
Complete USB debugging assistant
Wechat applet development (requesting background data and encapsulating request function)