时间:2021-07-01 10:21:17 帮助过:12人阅读
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title></head><body> <div itcast-controller="myController"> <input type="text" itcast-value="title"> <h1 itcast-cont="title"></h1> </div> <script> var model = { title:'我是标题', setTitle:function(title){ this.title = title; flush(); } }; function flush(){ //选择出来范围 var itcastScope = document.querySelector('[itcast-controller]'); //选择所有绑定了的元素 var itcastValueArr = document.querySelectorAll('[itcast-value]'); var itcastContArr = document.querySelectorAll('[itcast-cont]'); for(var i=0;i<itcastValueArr.length;i++){ itcastValueArr[i].value = model[itcastValueArr[i].getAttribute("itcast-value")]; itcastValueArr[i].oninput = function(){ model.setTitle(this.value); }; } for(var i=0;i<itcastValueArr.length;i++){ itcastContArr[i].innerHTML = model[itcastContArr[i].getAttribute("itcast-cont")]; } } flush(); </script></body></html>
相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!
推荐阅读:
Node.js代码的执行原理
angular独立作用域的使用概念
以上就是怎样用代码直观的表示mvvm原理的详细内容,更多请关注Gxl网其它相关文章!