@GivenCui
2016-08-11T13:26:45.000000Z
字数 5659
阅读 766
深入理解
要点:
变形 :
1.先变型:
2.尽量不要出现函数嵌套函数
3.可以有全局变量
4.把onload中不是赋值的语句放到单独函数中改成面向对象 :
1.全局变量就是属性
2.函数就是方法
3.Onload中创建对象
4.事件或定时器会改变this指向,尽量让面向对象中的this指向对象
注意事项
1. init() 中添加事件绑定后续所有功能函数 (方便改写事件类型)
2. 功能函数中 先恢复初值, 再实现功能
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>无标题文档</title><style>#div1 div,#div2 div{ width:200px; height:200px; border:1px #000 solid; display:none;}.active{ background:red;}</style><script>///////////////////////////////////////////////////////////////// ///// 第一步骤 : 面向过程实现功能 //////////////////////////////////////////////////////////////////////// window.onload = function () {// var box1 = document.querySelector("#div1");// var aBtn1 = box1.querySelectorAll("input");// var tabs1 = box1.querySelectorAll("div");// 方法一: for循环// for (var i = 0; i < aBtn1.length; i++) {// // 添加自定义index// aBtn1[i].index = i;// aBtn1[i].onclick = function () {// // 恢复所有原样式// for (var i = 0; i < aBtn1.length; i++) {// tabs1[i].style.display = "none";// aBtn1[i].className = "";// }// // 点击加样式// this.className = "active";// tabs1[this.index].style.display = "block";// }// }/*// 方法二: [].forEack.call()// 练习[].forEach.call()的这种[].forEach.call(aBtn1, function (ele,idx) {ele.index = idx;ele.onclick = function () {for (var i = 0; i < aBtn1.length; i++) {tabs1[i].style.display = "none";aBtn1[i].className = "";}ele.className = "active";tabs1[this.index].style.display = "block";}})*/// 方法三: for of// } // onload 结束///////////////////////////////////////////////////////////////// ///// 第二步骤 : 变形 ////////////////////////////////////////////////////////////////////////先变型://尽量不要出现函数嵌套函数//可以有全局变量//把onload中不是赋值的语句放到单独函数中// 第一组// ////////////////////全局变量///////////////////////////// var box1 = null;// var aBtn1 = null;// var tabs1 = null;// ///////////////////window.onload部分/////////////////// window.onload = function () {// // 第一组// box1 = document.querySelector("#div1")// aBtn1 = box1.querySelectorAll("input");// tabs1 = box1.querySelectorAll("div");// // 调用init函数// init()// }; // onload 结束// ///////////////事件绑定函数////////////////////////////// // init函数// // 借鉴模板设计模式// function init () {// for(var i = 0; i < aBtn1.length; i++) {// aBtn1[i].index = i;// // 点击事件调用change函数// aBtn1[i].onclick = change;// }// }// // change函数// function change () {// // 恢复默认样式// for ( var i=0; i < aBtn1.length; i++) {// aBtn1[i].className = "";// tabs1[i].style.display = "none";// }// // 事件触发样式// // 这两个this是onclick的aBtn1[i]// tabs1[this.index].style.display = "block";// this.className = "active";// }///////////////////////////////////////////////////////////////// ///// 第三步骤 : 面向对象封装 ////////////////////////////////////////////////////////////////////////改成面向对象://全局变量就是属性//函数就是方法//Onload中创建对象//改this指向问题 : 事件或者是定时器,尽量让面向对象中的this指向对象window.onload = function () {var box1 = document.querySelector("#div1");var aBtn1 = box1.querySelectorAll("input");var aDiv1 = box1.querySelectorAll("div");var t1 = new Tab(aBtn1, aDiv1);t1.init();}// 构造函数function Tab (a , b) {this.a = a;this.b = b;}// prototype添加方法// 模板方法// 事件在这里添加// 优点是很容易换事件的类型Tab.prototype.init = function () {var that = this;for(var i = 0; i < this.a.length; i ++) {this.a[i].index = i;// 这里的this因onclick而改变this.a[i].onclick = function () {// 函数是被onclick调用// 这里的this指代按钮that.change(this);};}};// 点击切换功能Tab.prototype.change = function (target) {var objA = this.a;var objB = this.b;// 恢复原样式for (var i = 0; i < objA.length; i++) {objA[i].className = "";objB[i].style.display = "none";}// 事件改变的样式target.className = "active";objB[target.index].style.display = "block";};</script></head><body><div id="div1"><input class="active" type="button" value="1"><input type="button" value="2"><input type="button" value="3"><div style="display:block">11111</div><div>22222</div><div>33333</div></div><div id="div2"><input class="active" type="button" value="1"><input type="button" value="2"><input type="button" value="3"><div style="display:block">11111</div><div>22222</div><div>33333</div></div></body></html>
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>无标题文档</title><style>#div1,#div2{width: 200px;}#div1 div,#div2 div{ width:200px; height:200px; border:1px #000 solid; display:none;background: green;}.active{ background:red;}</style><script>/*var arr = [4,7,1,3];arr.sort(); // 1 3 4 7var arr2 = [4,7,1,3];arr2.push(5);arr2.sort(); // 1 3 4 5 7*/window.onload = function(){var t1 = new Tab('div1');t1.init();// t1.autoPlay();var t2 = new Tab('div2');t2.init();t2.autoPlay();};function Tab(id){this.oParent = document.getElementById(id);this.aInput = this.oParent.getElementsByTagName('input');this.aDiv = this.oParent.getElementsByTagName('div');this.iNow = 0;}Tab.prototype.init = function(){var This = this;for(var i=0;i<this.aInput.length;i++){this.aInput[i].index = i;this.aInput[i].onclick = function(){This.change(this);};}};Tab.prototype.change = function(obj){for(var i=0;i<this.aInput.length;i++){this.aInput[i].className = '';this.aDiv[i].style.display = 'none';}obj.className = 'active';this.aDiv[obj.index].style.display = 'block';this.iNow = obj.index;};Tab.prototype.autoPlay = function(){var timer = null;clearInterval(timer);var This = this;timer = setInterval(function(){if(This.iNow == This.aInput.length-1){This.iNow = 0;} else {This.iNow++;}for(var i=0;i<This.aInput.length;i++){This.aInput[i].className = '';This.aDiv[i].style.display = 'none';}This.aInput[This.iNow].className = 'active';This.aDiv[This.iNow].style.display = 'block';},1000);This.oParent.onmouseover = function () {clearInterval(timer);};This.oParent.onmouseout = function () {This.autoPlay();};};</script></head><body><div id="div1"><input class="active" type="button" value="1"><input type="button" value="2"><input type="button" value="3"><div style="display:block">11111</div><div>22222</div><div>33333</div></div><div id="div2"><input class="active" type="button" value="1"><input type="button" value="2"><input type="button" value="3"><div style="display:block">11111</div><div>22222</div><div>33333</div></div></body></html>