JS 標籤頁切換(複雜)

來源:互聯網
上載者:User

標籤:classes   hit   UI   引用   調用   否則   檢查   names   har   

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>resize</title>
<style type="text/css">
*{margin:0;padding:0;}
 #wrap{width:500px; margin:10px auto; }
 #tag{ width:498px; overflow:hidden; background:#000; border:1px solid #000; }
 #tag li{list-style:none; float:left; margin-right:0px; color:white; padding:5px 20px; cursor: pointer;}
 #tag .current{ color:#000; background:#ccc; }
 #tagContent div{ width:498px; border:1px solid #000; border-top:none; height:300px; display:none; }
</style>
</head>
<body>
<div id="warp">
    <ul id="tag">
      <li>標籤一</li>
      <li>標籤二</li>
      <li>標籤三</li>
      <li>標籤4</li>
      <li>標籤5</li>
   </ul>
   <div id="tagContent">
      <div class="J_tabClass"> 內容一<br>內容一 </div>
      <div class="J_tabClass"> 內容二<br>內容二 </div>
      <div class="J_tabClass"> 內容三<br>內容三 </div>
      <div class="J_tabClass"> 內容4<br>內容4 </div>
      <div class="J_tabClass"> 內容4<br>內容5555555555555555 </div>
   </div>
</div>
<script type="text/javascript">
//nameSpace
var VVG = {};
VVG.DOM = {};
//建立getElementsByClassName方法
VVG.DOM.getElementsByClassName = function(str,parent,tag){
   //父節點存在
if(parent){
   // 當父節點為string類型時,尋找DOM節點,否則直接引用parent
      parent = typeof parent == "string" ? document.getElementById(parent):parent;
      // parent為空白時提示錯誤資訊
if(!parent) alert("找不到標籤,請檢查參數是否正確");
   }else{
      // 未傳值時,父節點為body
      parent = document.body;
   }
   // tagContent為節點類型,未傳值時為all節點
   tag = tag || "*";
   // 在父節點尋找子節點,建立空數組arr
var els = parent.getElementsByTagName(tag),arr=[];
   for(var i=0,n=els.length;i<n;i++){
      // 尋找每個節點下的classname,以空格分離為一個k數組
for(var j=0,k=els[i].className.split(" "),l=k.length;j<1;j++){
         // 當K數組中有一個值與str值相等時,記住這個標籤並推入arr數組
if(k[j]==str){
            arr.push(els[i]);
            break;
         }
      }
   }
   // 返回數組
return arr;
}
//建立TabManager對象
VVG.TabManager = function(oo){
         this.root = oo.root || "warp";
         this.tabUlId = oo.tabUlId||"tabUlId";
         this.tabConId = oo.tabConId||"tabConId";
         this.tabConClass = oo.tabConClass||"J_tabClass";
         this.trggle = oo.trggle || "click";
         this.currentCss = oo.currentCss || "current";
      }
VVG.TabManager.prototype = {
      tabChange:function(){
         // 擷取UL的id
var ulid = document.getElementById(this.tabUlId);
         // 擷取UL下的LI元素
var ulli = ulid.getElementsByTagName("li");
         // 擷取內容盒子DIV元素
var tabConId = document.getElementById(this.tabConId);
         // 擷取root下的div的class為特定值的對象數組
var tabConClasses = VVG.DOM.getElementsByClassName(this.tabConClass,this.root,"div");
         // 初始化
         ulli[0].className = this.currentCss;
         tabConClasses[0].style.display = ‘block‘;
         for (var i = ulli.length - 1; i >= 0; i--) {
            // 賦值this 與 that,避免this混亂
var that = this;
            // 設定對象index屬性
            ulli[i].index = i;
            // 設定對象的事件
            ulli[i]["on" + this.trggle] = function(){
               for(var j = tabConClasses.length - 1; j >= 0; j--){
                  tabConClasses[j].style.display = "none";
                  ulli[j].className = "";
               }
               tabConClasses[this.index].style.display = "block";
               this.className = that.currentCss;
            }
         }
      }
   }
// 建立對象執行個體
var TabManager1 = new VVG.TabManager(
   {
   root:"warp",//包裹tab對象的div層的ID名稱,必填
   tabUlId:"tag",//TAB標籤LI元素的父親UL元素的ID名稱,必填
   tabConId:"tagContent",//內容元素的包裹盒子的ID名稱,必填   
   trggle:"mouseover",//切換方式,預設為click,可選
   currentCss:"current",//TAB標籤liy元素的當前CSS名稱,預設為current
   tabConClass:"J_tabClass",//內容元素的CLASS名稱,預設為J_tabClass
   }
);
// 調用對象方法
TabManager1.tabChange();

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

JS 標籤頁切換(複雜)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.