用构造函数的方法写选项卡

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="tab">
<div class="btns">
<button type="button">1</button>
<button type="button">2</button>
<button type="button">3</button>
</div>
<div class="boxs">
<div id="">
1
</div>
<div id="" style="display: none;">
2
</div>
<div id="" style="display: none;">
3
</div>
</div>
</div>

<div id="tab2">
<div class="btns">
<button type="button">1</button>
<button type="button">2</button>
<button type="button">3</button>
</div>
<div class="boxs">
<div id="">
1
</div>
<div id="" style="display: none;">
2
</div>
<div id="" style="display: none;">
3
</div>
</div>
</div>

<script type="text/javascript">
//字面量
// 构造函数
// 类
//架构师 主程
function Tab(sel,index=0){
//属性
// 方法
this.sel = sel;//父元素的选择器
this.el = document.querySelector(this.sel)

this.btns = this.el.querySelectorAll(‘.btns button‘)
this.boxs = this.el.querySelectorAll(‘.boxs div‘)
this.selectedIndex = index;

//给按钮加事件
//外部 this tab对象,对象方法内部 也是tab对象
this.btnsEvent = function(){
for(let i=0;i<this.btns.length;i++){
this.btns[i].onclick = function(){
console.log(i)
console.log(this)//btn 事件内部的this 就是 出发事件的元素
this.selectBtnOfIndex(i)
this.selectBoxOfIndex(i)
}.bind(this)
}
}
this.selectBtnOfIndex= function(index){
for(let i=0;i<this.btns.length;i++){
this.btns[i].style.color = ‘‘
}
this.btns[index].style.color = ‘red‘
}
this.selectBoxOfIndex= function(index){
for(let i=0;i<this.boxs.length;i++){
this.boxs[i].style.display = ‘none‘
}
this.boxs[index].style.display = ‘block‘
}

console.log(1)
this.selectBtnOfIndex(this.selectedIndex)
this.selectBoxOfIndex(this.selectedIndex)
this.btnsEvent()

}

let t = new Tab(‘#tab‘,1)
let t2 = new Tab(‘#tab2‘,0)

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

原文地址:https://www.cnblogs.com/team-bali/p/11415337.html

时间: 2024-08-30 13:04:47

用构造函数的方法写选项卡的相关文章

js构造函数的方法与原型prototype

把方法写在构造函数内的情况我们简称为函数内方法,把方法写在prototype属性上的情况我们简称为prototype上的方法 函数内的方法: 使用函数内的方法我们可以访问到函数内部的私有变量,如果我们通过构造函数new出来的对象需要我们操作构造函数内部的私有变量的话, 我们这个时候就要考虑使用函数内的方法. prototype上的方法: 当我们需要通过一个函数创建大量的对象,并且这些对象还都有许多的方法的时候;这时我们就要考虑在函数的prototype上添加这些方法. 这种情况下我们代码的内存占

新方法写动画

现在必须用appframework.js 做各种效果, 语法是和jquery差不多的,没看到animate.slideUp这些, 貌似是有用自定义标签的属性来做的,还没有试过. 不过因为现在不用考虑浏览器兼容性,只要伺候chrome一位,这些东西用css3的transition就可以轻松搞定. 任何属性,加上transition,在改变设置过transition的属性值时就会有动画效果. 语法 transition: property duration timing-function delay

构造函数的方法和实例

function User(name, age) {    //构造函数模式 this.name = name; this.age = age; this.show = function () { return this.name  + ' '+this.age; }; } 要用时候用new运算符就可以了 var user1 = new User('小明', 30); 使用构造函数的方法,既解决了重复实例化的问题,又解决对象识别的问题. 要创建User对象的新实例,就要使用new操作符,使用这个

实战基础技能(02)-----------类,属性,构造函数和方法打死都要快速区分

一:做项目时经常会看见很对自定义的类,一定要快速的识别才能正确的理解项目代码 二:区分 类:很好区分,有Class修饰 方法:有返回值的描述,新的方法名 构造函数:没有返回值,有类名 属性:主要是get 和set标志,可能会有自定义的类型有一定的迷惑性 实战基础技能(02)-----------类,属性,构造函数和方法打死都要快速区分,布布扣,bubuko.com

CMake 基本使用方法--写CMakeList.txt

http://techbase.kde.org/Development/Tutorials/CMake_(zh_CN) http://www.cmake.org/Wiki/CMake 这一章将从软件开发者的角度来描述如何实用CMake.也就是说,如果你的目标是用CMake来管理你的生成过程,请阅读这一章. CMake的输入 COMMAND(args) 这里的 COMMAND 是命令行的名称,args是用空格分割的参数列表.典型的,对与每一个项目的目录存在一个CMakeLists.txt. 下面我

C#面向对象的方法写数组的功能

上一篇文章用Java方法写出了可以对数组执行的功能,然后在用实例化后的对象调用这些方法来实现这些功能: 这篇随笔改用C#语言实现同样的功能 方法类:Array 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication1 8 { 9 clas

js面向对象+一般方法的选项卡

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

C# - 通过构造函数的方法实现WinFrm程序开启时接收参数

通过构造函数的方法实现WinFrm程序开启时接收参数 在项目开发过程中,有时候我们需要获取程序开启时的参数,进而实现进一步的自动处理. 比如,我们通过运行(Win+R)执行如下命令: C:\tmp\1.exe ps1 ps2 ps3... 我们希望获取程序1.exe的入口参数,即ps1 ps2 ps3 ...,进而根据不同的参数,对相关业务逻辑进行不同的自动处理,那么我们如何实现呢? 两步实现 接下来我们将通过如下两步来实现: A.增加主窗体构造函数 public MainFrm() { Ini

JAVA构造函数与方法是啥意思

成员方法必须有返回类型即使是没有返回,也要写上void 构造函数没有返回类型,而且和类名一样!一个类里面,一看就知道了譬如:public class Test{public Test(){} //构造函数public Test(int i)(){} //构造函数public void test(){}//public int test2(){}//方法!}