页内区域栏目导航设计(标签Tab)

本文介绍如何使用CSS和JavaScript来实现网页上的Tab切换效果,包括样式设置、事件监听及区域显示隐藏等功能,适用于前端开发者学习。

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

<style>
.tabs {
    background:#09C;
    padding-left:10px;
    height:22px;
    padding-top:1px;
}
.tabs p {
    margin:1px 0 0 0;
}
.tab-normal {
    color:#FFF;
    line-height:20px;
    padding:4px 15px 4px 18px;
    border-right:1px solid #FFF;
    cursor:pointer;
}
.tab-hover {
    color: #FFF;
    background:#6CF;
    line-height:20px;
    padding:4px 15px 4px 18px;
    border-right:1px solid #FFF;
    cursor:pointer;
}
.tab-current {
    background:#6CF;
    line-height:20px;
    font-weight:700;
    padding:4px 15px 4px 18px;
    border-right:2px solid #FC6;
    cursor:pointer;
}
</style>

<script language="javascript">
<!--
var JS = new Object();
JS.srcElement = function( e ) {
    if ( typeof e == "undefined" ) e = window.event;
    var src = document.all ? e.srcElement : e.target;
    return src;
}
JS.parentElement = function( o ) {
    if ( typeof o == "undefined" ) return;
    var parent = document.all ? o.parentElement : o.parentNode;
    return parent;
}
JS.childNodes = function( o ) {
 var oChildNodes = o.childNodes;
 var oNodes = new Array();
 var j = 0;
 for ( var i = 0; i < oChildNodes.length; i++ ) {
  if ( typeof oChildNodes[i].tagName == 'undefined' ) continue;
  oNodes[j++] = oChildNodes[i];
 }
 return oNodes;
}
JS.id = function( id ) {
 return document.getElementById( id );
}
var Tab = function( sTab, sArea ) {
 this._sName = sTab;
 this._sAreaName = sArea;
 this._sClassNormal = 'tab-normal';
 this._sClassHover = 'tab-hover';
 this._sClassCurrent = 'tab-current';
 this._sPrefix = 'tabs_';
 this._sAreaPrefix = 'tab_';
 
 this.setClass = function( sNormal, sHover, sCurrent ) {
  this._sClassNormal = sNormal;
  this._sClassHover = sHover;
  this._sClassCurrent = sCurrent;
 }
 this.setPrefix = function( sPrefix, sAreaPrefix ) {
  this._sPrefix = sPrefix;
  this._sAreaPrefix = sAreaPrefix;
 }
 this.exgAreaClass = function( oSrc ) {
  var oArea = JS.id( this._sAreaName );
  var oChildNodes = JS.childNodes( oArea );
  for ( var i = 0; i < oChildNodes.length; i++ ) {
   if ( eval( "/^" + this._sAreaPrefix + "/" ).test( oChildNodes[i].id ) ) {
    if ( oChildNodes[i].id == this._sAreaPrefix + oSrc.id.substr( oSrc.id.indexOf( "_" ) + 1 ) )
     oChildNodes[i].style.display = 'block';
    else oChildNodes[i].style.display = 'none';
   }
  }
 }
 this.exgClass = function( oSrc ) {
  var oParent = JS.parentElement( oSrc );
  var oChildNodes = JS.childNodes( oParent );
  for ( var i = 0; i < oChildNodes.length; i++ ) {
   if ( eval( "/^" + this._sPrefix + "/" ).test( oChildNodes[i].id ) ) {
    if ( oChildNodes[i].id == oSrc.id )
     oChildNodes[i].className = this._sClassCurrent;
    else oChildNodes[i].className = this._sClassNormal;
   }
  }
 }
 TabEvents( this );
}
var TabEvents = function( oTab ) {
 this._oTab = JS.id( oTab._sName );
 this._oTab.onmouseover = function( e ) {
  var obj = JS.srcElement( e );
  if ( obj.className == oTab._sClassNormal ) obj.className = oTab._sClassHover; 
 }
 this._oTab.onmouseout = function( e ) {
  var obj = JS.srcElement( e );
  if ( obj.className == oTab._sClassHover ) {
   obj.className = oTab._sClassNormal;
  }
 }
 this._oTab.onclick = function( e ) {
  var oSrc = JS.srcElement( e );
  if ( !eval( "/^" + oTab._sPrefix + "/" ).test( oSrc.id ) ) return;
  oTab.exgAreaClass( oSrc );
  oTab.exgClass( oSrc );
 }
}
//-->
</script>
<div id="tabs_main" class="tabs"><p>
<span class="tab-current" id="tabs_test1">1</span>
<span class="tab-normal" id="tabs_test2">2</span>
<span class="tab-normal" id="tabs_test3">3</span>
</p></div>
<script language="javascript">
<!--
var oTab = new Tab( "tabs_main", "tab_main" );
//oTab.setClass( sNormal, sHover, sCurrent );
//oTab.setPrefix( sPrefix, sAreaPrefix );
//-->
</script>
<div id="bottom">
<form method="post" action="">
<div id="tab_main">
<div id="tab_test1" style="display:block">
<table><tbody>
<tr><td>
 <input type="text" name="t1" value="" />
</td></tr>
<tr><td>
 <input type="button" name="sub" value="GO" />
</td></tr>
</tbody></table>
</div>
<div id="tab_test2" style="display:none">
test22
</div>
<div id="tab_test3" style="display:none">
test333
</div>
</div>
</form>
</div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值