zoukankan      html  css  js  c++  java
  • JS 双向数据绑定、单项数据绑定

    简单的双向数据绑定

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
    <input type="text" id="aa"/>
    <span id="bb" style="border: 2px solid orange;margin-left: 20px;">{{hello}}</span>
    <br>
    <br>
    <input type="text" id="cc"/>
    
    <script>
        // 双向数据绑定的原理:属性拦截
        // 属性拦截实现方式 : 使用Object.defineProperty()将对象的属性变成访问器属性。
    
        var obj = {};
        Object.defineProperty(obj, 'hello', {
            enumerable: true,
            configurable: true,
            get: function () { // 拿到最新的值
                return document.getElementById('aa').value;
            },
            set: function (val) { // 设置最新的值
                document.getElementById('bb').innerHTML = obj.hello;
                document.getElementById('cc').value = obj.hello;
            }
        });
        Object.defineProperty(obj, 'hello2', {
            enumerable: true,
            configurable: true,
            get: function () {
                return document.getElementById('cc').value;
            },
            set: function (val) {
                document.getElementById('aa').value = obj.hello2;
                document.getElementById('bb').innerHTML = obj.hello2;
            }
        });
        document.getElementById('aa').onkeyup = function () {
            obj.hello = this.value;
        };
        document.getElementById('cc').onkeyup = function () {
            obj.hello2 = this.value;
        };
        obj.hello = ""; //属性名必须设置为空,否则在使用插值表达式的时候直接会把插值表达式显示出来
        obj.hello2 = ""; //属性名必须设置为空,否则在使用插值表达式的时候直接会把插值表达式显示出来
    </script>
    </body>
    </html>

    单项数据绑定

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
    <input type="text" id="aa"/>
    <span id="bb" style="border: 2px solid orange;margin-left: 20px;">{{hello}}</span>
    
    <script>
        // 双向数据绑定的原理:属性拦截
        // 属性拦截实现方式 : 使用Object.defineProperty()将对象的属性变成访问器属性。
    
        var obj = {};
        Object.defineProperty(obj, 'hello', {
            enumerable: true,
            configurable: true,
            get: function () {
                return document.getElementById('aa').value;
            },
            set: function (val) {
                document.getElementById('bb').innerHTML = obj.hello;
            }
        });
    
        document.getElementById('aa').onkeyup = function () {
            obj.hello = this.value;
        };
    
        obj.hello = ""; //属性名必须设置为空,否则在使用插值表达式的时候直接会把插值表达式显示出来
    </script>
    </body>
    </html>
  • 相关阅读:
    Masterha-manager避免自动关闭的方法
    MHA自动切换流程
    vue 使用keep-alive缓存tab切换组件,保持每个组件滚动条位置
    el-select 输入下拉搜索,匹配不到数据时也保留输入值,同时input获取焦点时保留其value值
    尝试 React16、React-router4 实现根据动态菜单生成按需加载的路由
    vue iscroll5滚动条组件
    vue项目中 axios请求拦截器与取消pending请求功能
    jquery编写的简单日历
    手机访问电脑wampServer本地环境页面
    ajax在ie下返回未定义解决方案
  • 原文地址:https://www.cnblogs.com/haohaogan/p/15700295.html
Copyright © 2011-2022 走看看