jsを使用してデータを双方向にバインド

4182 ワード

<!DOCTYPE html>
<html>
<body>
<input type="text" id="input"/>
<p id="showText"></p>

<script>

  var obj = {};
  Object.defineProperty(obj,"newProp",{
    get:function(){
      return obj;
    },
    set:function(newVal){
      document.getElementById("input").value = newVal;
      document.getElementById("showText").innerHTML = newVal;
    }
  })
  document.addEventListener("keyup",function(e){
    obj.newProp = e.target.value;
  })

</script>
</body>
</html>