溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何實現(xiàn)JS原生數(shù)據(jù)雙向綁定

發(fā)布時間:2021-08-02 13:59:15 來源:億速云 閱讀:121 作者:小新 欄目:web開發(fā)

這篇文章主要介紹如何實現(xiàn)JS原生數(shù)據(jù)雙向綁定,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

代碼如下:

<span  deep="7"><!DOCTYPE html> 
<html lang="en"> <head> 
  <meta charset="UTF-8"> 
  <title>Demo</title> 
  <script> 
    function DataBinder( object_id ) { 
      // Create a simple PubSub object 
      var pubSub = { 
            callbacks: {}, 
            on: function( msg, callback ) { 
              this.callbacks[ msg ] = this.callbacks[ msg ] || []; 
              this.callbacks[ msg ].push( callback ); 
            }, 
            publish: function( msg ) { 
              this.callbacks[ msg ] = this.callbacks[ msg ] || []; 
              for ( var i = 0, len = this.callbacks[ msg ].length; i < len; i++ ) { 
                this.callbacks[ msg ][ i ].apply( this, arguments ); 
              } 
            } 
          }, 
          data_attr = "bind-" + object_id, 
          message = object_id + ":input", 
          timeIn; 
          changeHandler = function( evt ) { 
            var target = evt.target || evt.srcElement, // IE8 compatibility 
                prop_name = target.getAttribute( data_attr ); 
            if ( prop_name && prop_name !== "" ) { 
              clearTimeout(timeIn); 
              timeIn = setTimeout(function(){ 
                pubSub.publish( message, prop_name, target.value ); 
              },50); 
            } 
          }; 
      // Listen to change events and proxy to PubSub 
      if ( document.addEventListener ) { 
        document.addEventListener( "input", changeHandler, false ); 
      } else { 
        // IE8 uses attachEvent instead of addEventListener 
        document.attachEvent( "oninput", changeHandler ); 
      } 
      // PubSub propagates changes to all bound elements 
      pubSub.on( message, function( evt, prop_name, new_val ) { 
        var elements = document.querySelectorAll("[" + data_attr + "=" + prop_name + "]"), 
            tag_name; 
        for ( var i = 0, len = elements.length; i < len; i++ ) { 
          tag_name = elements[ i ].tagName.toLowerCase(); 
          if ( tag_name === "input" || tag_name === "textarea" || tag_name === "select" ) { 
            elements[ i ].value = new_val; 
          } else { 
            elements[ i ].innerHTML = new_val; 
          } 
        } 
      }); 
      return pubSub; 
    } 
    function DBind( uid ) { 
      var binder = new DataBinder( uid ), 
      user = { 
        // ... 
        attributes: {}, 
        set: function( attr_name, val ) { 
          this.attributes[ attr_name ] = val; 
          // Use the `publish` method 
          binder.publish( uid + ":input", attr_name, val, this ); 
        }, 
        get: function( attr_name ) { 
          return this.attributes[ attr_name ]; 
        }, 
        _binder: binder 
      }; 
      // Subscribe to the PubSub 
      binder.on( uid + ":input", function( evt, attr_name, new_val, initiator ) { 
        if ( initiator !== user ) { 
          user.set( attr_name, new_val ); 
        } 
      }); 
      return user; 
    } 
  </script> 
</head> 
<body> 
<input type="text" bind-1="name" /> 
<span bind-1="name"></span> 
<script> 
  var DBind = new DBind( 1 ); 
  DBind.set( "name", "" ); 
</script> 
</body> 
</html>
</span>

效果示例:

如何實現(xiàn)JS原生數(shù)據(jù)雙向綁定

以上是“如何實現(xiàn)JS原生數(shù)據(jù)雙向綁定”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

js
AI