js#原生#双向绑定案例1

    技术2024-11-22  20

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <div> <input id="input" type="text" name="" id="" value="" /> <span id="bind"></span> </div> </body> <script type="text/javascript"> var input = document.getElementById('input') var bind = document.getElementById('bind') </script> <script type="text/javascript"> var model = new Object(); var val = ''; // 对 val 赋值,修改 dom Object.defineProperty(model, 'value', { get: function() { return val; }, set: function(v) { input.value = v; bind.innerHTML = v; val = v; } }) //鼠标事件,检查 val的属性值 input.addEventListener('keyup', function(e) { model.value = e.target.value; }) console.log(model.value) </script> </html>
    Processed: 0.036, SQL: 9