實現(xiàn)簡單的雙向數(shù)據(jù)綁定

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0, viewport-fit=cover" />
  <meta name="format-detection" content="telephone=no" />
  <meta HTTP-EQUIV="Pragma" CONTENT="no-cache">
  <meta HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
  <meta HTTP-EQUIV="Expires" CONTENT="0">
<script src="https://cdn.bootcss.com/bignumber.js/9.0.0/bignumber.js"></script>
  <title></title>
  <script>
    (function (hasTitle, psdWidth) {
      psdWidth = psdWidth || 640;
      var zoomStyle = document.getElementById('zoom-style');
      var landscape = '';
      /*橫屏*/
      var portrait = '';
      /*豎屏*/
      if (!zoomStyle) {
        zoomStyle = document.createElement('style');
        zoomStyle.id = 'zoom-style';
        zoomStyle.type = 'text/css';
        document.documentElement.appendChild(zoomStyle);
      }
      var timer = null;
      _resize();
      window.addEventListener('orientationchange', resize, false);

      function resize() {
        clearTimeout(timer);
        timer = setTimeout(_resize, 300);
      }

      function _resize() {
        var screen_width = document.documentElement.clientWidth;
        if (!screen_width) {
          /*qq中有幾率為0*/
          setTimeout(_resize, 50);
          return;
        }
        var screen_height = window.innerHeight;
        if (window.innerWidth !== screen_width) {
          screen_height *= screen_width / window.innerWidth;
        }
        var fontSize = screen_width / (psdWidth / 2) * 50;
        if (screen_width > screen_height) {
          landscape = '@media all and (orientation : landscape){html{font-size:' + fontSize + 'px !important;}}';
        } else {
          var scaleSize = Math.min(1, screen_height / screen_width * (hasTitle ? 750 / 1207 : 750 / 1334));
          portrait = 'html{font-size:' + fontSize + 'px !important;}' + (scaleSize < 1 ?
            '.auto_scale{-webkit-transform: scale(' + scaleSize + ');transform: scale(' + scaleSize +
            ');}.auto_scale_center{-webkit-transform:translate(-50%,-50%) scale(' + scaleSize +
            ');transform:translate(-50%,-50%) scale(' + scaleSize + ');}' :
            '.auto_scale_center{-webkit-transform:translate(-50%,-50%);transform:translate(-50%,-50%);}');
        }
        zoomStyle.innerHTML = portrait + landscape;
        if (landscape && portrait) {
          window.removeEventListener('orientationchange', resize);
        }
      
      }
    })(true, 750);

  </script>
</head>

<body>

    <div id="vue-demo"></div>
    <input type="text" id="input">
    <script>
        var obj  = {};
        var Demo = document.getElementById('vue-demo')
        var Inp = document.getElementById("input")
        Object.defineProperty(obj, 'name', {
            get: function() {
                return val;
            },
            set: function (newVal) {   //當該屬性被賦值的時候觸發(fā)
                Inp.value = newVal;
                Demo.innerHTML = newVal;
            }
        })
        input.addEventListener('input', function(e) {
          console.log(e);
            // 給obj的name屬性賦值,進而觸發(fā)該屬性的set方法
            obj.name = e.target.value;
        });
        obj.name = 'yzg';//在給obj設置name屬性的時候,觸發(fā)了set這個方法
    </script>
</body>

</html>
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容