js使用面向对象实现选项卡
2016-01-29 08:50
706 查看
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>选项卡</title> <style> #div1{ height: 500px; width: 500px; margin:0 auto; } #div1 input{ background-color: #8a7dbe; } #div1 input.active{ background-color: #4CAE09; } #div1 div{ width: 300px; height:200px; display: none; } #div1 div.active1{ background-color: #2aabd2; } </style> </head> <body> <div id="div1"> <input type="button" value="aaa"> <input type="button" value="bbb"> <input type="button" value="ccc"> <div class="active1" style="display: block">aaa</div> <div>bbb</div> <div>ccc</div> </div> </body> <script> //使用面向对象思想实现选项卡 window.onload= function () { new load('div1'); }; function load(id) { //构造函数 var _this=this; //this指当前的新建对象,div1 var oDiv = document.getElementById(id); this.aBtn = document.getElementsByTagName('input'); //abtn变为当前属性 this.aDiv = oDiv.getElementsByTagName('div'); for (var i = 0; i < this.aBtn.length; i++) { this.aBtn[i].index = i; this.aBtn[i].onclick = function() { _this.click(this); //this指abtn[i] } } } load.prototype.click= function(oBtn) { //用原型添加方法 for(var j=0;j<this.aBtn.length;j++){ //this指div1 this.aBtn[j].className=''; this.aDiv[j].style.display='none'; } this.className='active'; this.aDiv[oBtn.index].style.display='block'; this.aDiv[oBtn.index].className='active1'; } </script> </html>
相关文章推荐
- JavaScript中的this,call,apply使用及区别详解
- javascript实现tab响应式切换特效
- js获取前几天日期
- 归并排序c++和javascript实现
- javascript Math.pow 函数 详解 【附】年均增长率计算
- 原生JavaScript实现跨域
- #学习笔记#(28)+JS瀑布流-滚动条下拉加载图片
- js中的typeof
- three.js 源码注释(四十四)Light/DirectionalLight.js
- javascript实现tab响应式切换特效
- JavaScript中的this,call,apply使用及区别详解
- js实现滚动条滚动到某个位置便自动定位某个tr
- 基于javascript实现listbox左右移动
- JavaScript判断DIV内容是否为空的方法
- javascript实现表单验证
- 动态创建按钮的JavaScript代码
- JavaScript学习总结之JS、AJAX应用
- 理解Javascript文件动态加载
- 详解javascript实现瀑布流绝对式布局
- 详解javascript实现瀑布流列式布局