选项卡面向对象写法

本文介绍了一个简单的网页选项卡切换效果实现方法,通过面向对象的JavaScript编程方式完成按钮点击与对应内容显示隐藏的功能。代码中使用了HTML、CSS进行页面布局与样式设置,并通过JavaScript实现了动态交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>选项卡面向对象写法</title>
<style>
#div1 .active{background: yellow;}
#div1 div{height: 200px; width: 200px; background: #ccc; display: none;}
</style>
<script>
//混合模式
window.onload=function()
{
new TabSwitch('div1');
};
function TabSwitch(id)//构造函数
{
var _this=this;
var oDiv1=document.getElementById(id);
this.aIut=oDiv1.getElementsByTagName('input');
this.aDiv=oDiv1.getElementsByTagName('div');
for(var i=0;i<this.aIut.length;i++)
{
this.aIut[i].index=i; //index属性可返回下拉列表中选项的索引位置
this.aIut[i].onclick=function()
{
_this.fnClick(this);
};
}
};

TabSwitch.prototype.fnClick=function(oBtn)//原型
{
for(var i=0;i<this.aIut.length;i++)
{
this.aIut[i].className='';
this.aDiv[i].style.display='none';
}
oBtn.className='active';
this.aDiv[oBtn.index].style.display='block';
};
</script>
</head>
<body>
<div id="div1">
<input type="button" value="1" class="active"/>
<input type="button" value="2" />
<input type="button" value="3" />
<div style="display: block">周杰伦</div>
<div>张学友</div>
<div>薛之谦</div>
</div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值