最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

原生javascript實現(xiàn)Tab選項卡切換功能

 更新時間:2015年01月12日 09:38:25   投稿:hebedich  
本文主要介紹了使用原生javascript實現(xiàn)Tab選項卡切換的功能,雖然jQuery有很多類似的插件,單jQuery庫著實有點龐大,這種小功能還是直接用javascript來做就好了。

分析個人用原生JS獲取類名元素的代碼:

復(fù)制代碼 代碼如下:

getByClassName:function(className,parent){
            var elem = [],
                node = parent != undefined&&parent.nodeType==1?parent.getElementsByTagName('*'):document.getElementsByTagName('*'),
                p = new RegExp("(^|\\s)"+className+"(\\s|$)");
            for(var n=0,i=node.length;n<i;n++){
                if(p.test(node[n].className)){
                    elem.push(node[n]);
                }
            }
            return elem;
        }

  parent參數(shù)是可選的,但需要先判斷它是否存在,且是節(jié)點dom元素 parent != undefined&&parent.nodeType==1 ,nodeType == 1可以判斷節(jié)點是否為dom元素,在火狐瀏覽器里面,空白也算是節(jié)點(.childnodes),用這個屬性就判斷是否為dom元素,排除空白符.

移除元素的類名:

復(fù)制代碼 代碼如下:

var cur = new RegExp(this.sCur,'g');  //this.sCur就是類名,這里是用變量保存 如:this.sCur = "cur";
           this.oTab_btn[n].className = this.oTab_btn[n].className.replace(cur,'');        

調(diào)用例子:

復(fù)制代碼 代碼如下:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
    body,p,ul,li {padding: 0;margin: 0;}
    ul {list-style: none;}   
    h3 {padding: 5px;background-color: #999;margin-bottom: 10px;}
    pre {border: 1px dotted #000;}
    .explan {padding: 10px;color: #333;line-height: 1.6;}
    .box {width: 300px;height:100px;border: 1px solid #ccc;}
    .box ul{height: 30px;line-height: 30px;}
    .box ul li {float: left;display: inline;width: 150px;text-align: center;background-color: #eee;cursor: pointer;}
    .box .tab-cur {background-color: #000;color: #fff;}
    .box p {display: none;padding: 30px;}
    /*tabB*/
    #tabB {width: 450px;}
    .box .tab-cur02 {background-color: #025023;}
    </style>
</head>
<body>
    <div class="explan">
        <strong>使用閱讀 :</strong> <br>
            {'tabBtn':'#tabA .tab-i','tabCon':'#tabA .tab-c','cur':'tab-cur'} 【必選】 <br>
          (1)'tabBtn':'#tabA .tab-i','tabCon':'#tabA .tab-c'  選擇器:只支持 #id .className,(ID + 空格 + 類名) 【必選】<br>
         ?。?)'cur':'tab-cur'(默認) :為切換按鈕當前狀態(tài)(類名)【必選】<br>
          (3)'type':'mouseover'|| 'clicl' 默認是點擊 【可選】
    </div>
    <h3>tabA</h3>
     <pre>new LGY_tab({'tabBtn':'#tabA .tab-i',
    'tabCon':'#tabA .tab-c',
    'cur':'tab-cur'
    });
         </pre>
    <div class="box" id="tabA">
        <ul>
            <li class="tab-i">btn-A</li>
            <li class="tab-i">btn-B</li>
        </ul>
        <p class="tab-c">con-A</p>
        <p class="tab-c">con-B</p>
    </div>

    <h3>tabB</h3>
     <pre>new LGY_tab({'tabBtn':'#tabB .tab-i',
    'tabCon':'#tabB .tab-k',
    'cur':'tab-cur02',
    'type':'mouseover'
    });
         </pre>
    <div class="box" id="tabB">
        <ul>
            <li class="tab-i">btn-A</li>
            <li class="tab-i">btn-B</li>
            <li class="tab-i">btn-C</li>
        </ul>
        <p class="tab-k">con-A</p>
        <p class="tab-k">con-B</p>
        <p class="tab-k">con-C</p>
    </div>
    <script type="text/javascript" src="下方的代碼段.js"></script>
    <script type="text/javascript">
    //
    new LGY_tab({'tabBtn':'#tabA .tab-i',
                 'tabCon':'#tabA .tab-c',
                 'cur':'tab-cur'
    });
    //
    new LGY_tab({'tabBtn':'#tabB .tab-i',
                 'tabCon':'#tabB .tab-k',
                 'cur':'tab-cur02',
                 'type':'mouseover'
    });
    //test
    //
    new LGY_tab({'tabBtn':'#tabB .tab-j',
                 'tabCon':'#tabB .tab-k',
                 'cur':'tab-cur02',
                 'type':'mouseover'
    });
    </script>
</body>
</html>

JS詳細代碼:

復(fù)制代碼 代碼如下:

function LGY_tab(option){
    this.oTab_btn = this.getDom(option.tabBtn);//切換點擊的元素
    this.oTab_clist = this.getDom(option.tabCon); //切換的內(nèi)容
    if(!this.oTab_btn || !this.oTab_clist) return;
    this.sCur = option.cur; //激活的狀態(tài)
    this.type = option.type || 'click';
    this.nLen = this.oTab_btn.length;
    this.int();
}
LGY_tab.prototype = {
    getId:function(id){
        return document.getElementById(id);
    },
    getByClassName:function(className,parent){
        var elem = [],
            node = parent != undefined&&parent.nodeType==1?parent.getElementsByTagName('*'):document.getElementsByTagName('*'),
            p = new RegExp("(^|\\s)"+className+"(\\s|$)");
        for(var n=0,i=node.length;n<i;n++){
            if(p.test(node[n].className)){
                elem.push(node[n]);
            }
        }
        return elem;
    },
    getDom:function(s){
        var nodeName = s.split(' '),
            p = this.getId(nodeName[0].slice(1)),
            c = this.getByClassName(nodeName[1].slice(1),p);
        if(!p || c.length==0) return null;
        return c;
    },
    change:function(){
        var cur = new RegExp(this.sCur,'g');
        for(var n=0;n<this.nLen;n++){
            this.oTab_clist[n].style.display = 'none';
            this.oTab_btn[n].className = this.oTab_btn[n].className.replace(cur,'');
        }
    },
    int:function(){
        var that = this;
        this.oTab_btn[0].className += ' '+this.sCur;
        this.oTab_clist[0].style.display = 'block';
        for(var n=0;n<this.nLen;n++){
            this.oTab_btn[n].index = n;
            this.oTab_btn[n]['on'+this.type] = function(){
                that.change();
                that.oTab_btn[this.index].className +=' ' + that.sCur;
                that.oTab_clist[this.index].style.display = 'block';
            }
        }
    }
}

最終效果圖展示:

效果是不是很棒呢,而且兼容性也不錯,代碼也很簡潔,完全可以替代龐大的jQuery選項卡切換插件了。

相關(guān)文章

  • bootstrapvalidator之API學習教程

    bootstrapvalidator之API學習教程

    這篇文章為大家分享了bootstrapvalidator之API學習教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • js實現(xiàn)圖片淡入淡出效果

    js實現(xiàn)圖片淡入淡出效果

    這篇文章主要為大家詳細介紹了js實現(xiàn)圖片淡入淡出效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • JavaScript進制轉(zhuǎn)換實現(xiàn)方法解析

    JavaScript進制轉(zhuǎn)換實現(xiàn)方法解析

    這篇文章主要介紹了JavaScript進制轉(zhuǎn)換實現(xiàn)方法,結(jié)合實例形式分析了JavaScript進制轉(zhuǎn)換中十進制與其他進制轉(zhuǎn)換、以及隨機顏色生成相關(guān)操作技巧,需要的朋友可以參考下
    2020-01-01
  • Js組件的一些寫法

    Js組件的一些寫法

    今天看了rank的一篇javascript腳本控件topic,突然想總結(jié)一下一些寫JS組件的方法,或者說一些不同人的不同coding style。
    2010-09-09
  • WebStorm中如何將自己的代碼上傳到github示例詳解

    WebStorm中如何將自己的代碼上傳到github示例詳解

    這篇文章主要介紹了WebStorm中如何將自己的代碼上傳到github,本文分步驟通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • 跟我學習javascript創(chuàng)建對象(類)的8種方法

    跟我學習javascript創(chuàng)建對象(類)的8種方法

    跟我學習javascript創(chuàng)建對象(類)的8種方法,每一種方法都有詳細的介紹,不知道javascript如何創(chuàng)建對象的朋友,不要錯過這篇文章。
    2015-11-11
  • Javascript實現(xiàn)時間倒計時功能

    Javascript實現(xiàn)時間倒計時功能

    這篇文章主要為大家詳細介紹了Javascript實現(xiàn)時間倒計時功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • 提高 DHTML 頁面性能

    提高 DHTML 頁面性能

    提高 DHTML 頁面性能...
    2006-12-12
  • 你可能不需要在JavaScript使用switch語句

    你可能不需要在JavaScript使用switch語句

    這篇文章主要介紹了你可能不需要在JavaScript使用switch語句,對switch性能感興趣的同學,可以參考下
    2021-04-04
  • JS拖拽插件實現(xiàn)步驟

    JS拖拽插件實現(xiàn)步驟

    實現(xiàn)JS拖拽插件主要從六個方面做介紹:一、js拖拽插件的原理,二、根據(jù)原理實現(xiàn)的最基本效果,三、代碼抽象與優(yōu)化,四、擴展:有效的拖拽元素,五、性能優(yōu)化和總結(jié),六、jquery插件化 ,需要的朋友可以參考下
    2015-08-08

最新評論

遂宁市| 信丰县| 怀集县| 航空| 淳安县| 张掖市| 永川市| 东乌珠穆沁旗| 加查县| 桐梓县| 孟州市| 敦煌市| 龙井市| 青浦区| 游戏| 宜黄县| 宁都县| 迭部县| 桃源县| 青龙| 十堰市| 公安县| 大丰市| 瑞丽市| 灵石县| 肇州县| 舞钢市| 贵阳市| 天台县| 平度市| 双牌县| 友谊县| 综艺| 和平区| 苍溪县| 江山市| 响水县| 璧山县| 铜川市| 沁阳市| 仪陇县|