当前位置:网站首页>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
边栏推荐
- Uniapp mobile terminal uses canvas to draw background convex arc
- [turn] explain awk (1)__ Awk Basics_ Options_ Program segment parsing and examples
- Intelligent digital asset management helps enterprises win the post epidemic Era
- Printf function
- Leetcode- divide candy - simple
- Download and installation of universal player potplayer, live stream m3u8 import
- Uniapp dynamically shows / hides the navigation bar return button
- USB status error and its cause (error code)
- Leetcode- number of words in string - simple
- Uni app disable native navigation bar
猜你喜欢

1+1 > 2, share creators can help you achieve

Wechat applet: click the event to obtain the current device information (basic)

Hbuilderx: installation of hbuilderx and its common plug-ins

The boys x pubgmobile linkage is coming! Check out the latest game posters

Add attributes in storyboard and Xib (fillet, foreground...) Ibinspectable and ibdesignable

MySQL stored procedure

自定义View —— 可伸展的CollapsExpendView

智能数字资产管理助力企业决胜后疫情时代

Echart柱状图:堆叠柱状图显示value
![A glimpse of [wechat applet]](/img/ef/17564759e720b06c2bd496d1db7544.jpg)
A glimpse of [wechat applet]
随机推荐
Leetcode- longest continuous increasing sequence - simple
High burst solution 2
Fichier local second Search Tool everything
Leetcode- divide candy - simple
[turn] explain awk (1)__ Awk Basics_ Options_ Program segment parsing and examples
Not in the following list of legal domain names, wechat applet solution
Hbuilderx: installation of hbuilderx and its common plug-ins
Echart histogram: stack histogram value formatted display
[compilation and assembly link] coff file and structure description
Self summarizing
Leetcode- maximum average of subarray i- simple
Echart柱状图:堆叠柱状图显示value
AI实现亲人“复活”|老照片修复|老照片上色,免费APP推荐
Echart折线图:当多条折线图的name一样时也显示不同的颜色
Minimum spanning tree (prim+kruskal) learning notes (template +oj topic)
Leetcode longest harmonic subsequence simple
Echart折线图:当多条折线存在相同name,图例仍全部显示
Turn to 2005
A brief analysis of the overall process of view drawing
安全基线检查脚本—— 筑梦之路