zoukankan      html  css  js  c++  java
  • 5选项卡(封装插件版)加事件委托版选项卡

    HTML代码(两款HTML选项卡代码):

    第一款:非事件委托版HTML代码:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>非事件委托</title>
        <style type="text/css">
            * {
                margin: 0;
                padding: 0;
                color: #424242;
                font-size: 14px;
            }
    
            ul, li {
                list-style: none;
            }
    
            .tabBox {
                margin: 10px auto;
                width: 600px;
                overflow: hidden;
            }
    
            .tabBox ul {
                position: relative;
                top: 1px;
                overflow: hidden;
            }
    
            .tabBox ul li {
                /*float: left;*/
                display: inline-block;
                margin-right: 10px;
                width: 80px;
                height: 30px;
                line-height: 30px;
                text-align: center;
                cursor: pointer;
                border: 1px solid green;
            }
    
            .tabBox ul li.bg {
                background: lightblue;
                border-bottom-color: lightblue;
            }
    
            .tabBox div {
                display: none;
                height: 100px;
                line-height: 100px;
                text-align: center;
                border: 1px solid green;
                background: lightblue;
    
            }
    
            .tabBox div.bg {
                display: block;
            }
        </style>
    </head>
    <body>
    <div class="tabBox">
        <ul>
            <li class="bg">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
    
        </ul>
        <div class="bg">1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
    </div>
    
    <div class="tabBox">
        <ul>
            <li class="bg">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
    
        </ul>
        <div class="bg">1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
    </div>
    
    <script charset="UTF-8" type="text/javascript" src="js/utils.min.js"></script>
    <script type="text/javascript">
        var allTabBox = utils.getElementsByClass("tabBox");
        for(var i = 0; i<allTabBox.length;i++){
            tabChange(allTabBox[i]);
        }
        //->选项卡切换的操作封装成插件
        function tabChange(tabBox){
            //var tabBox = document.getElementById("curId");
            var tabBoxUl = utils.firstChild(tabBox);//->第一个ul
            var oLis = utils.children(tabBoxUl, "li");//->第一个ul所有子元素的li
    
            var divList = utils.children(tabBox, "div");//->所有容器子元素中的div
    
            for (var i = 0; i < oLis.length; i++) {
                var curLi = oLis[i];
                curLi.index = i;
                curLi.onclick = function () {
                    //->
                    utils.addClass(this, "bg");
                    var curLiSibling = utils.siblings(this);//当前li的所有兄弟
                    for (var k = 0; k < curLiSibling.length; k++) {
                        utils.removeClass(curLiSibling[k], "bg");//兄弟的都移除掉
                    }
                    //->
                    for (k = 0; k < divList.length; k++) {
                        k === this.index ? utils.addClass(divList[k], "bg") : utils.removeClass(divList[k], "bg");
                    }
    
                };
            }
    
        }
    </script>
    </body>
    </html>

    第二款:事件委托版HTML代码:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>事件委托/事件代理</title>
        <style type="text/css">
            * {
                margin: 0;
                padding: 0;
                color: #424242;
                font-size: 14px;
            }
    
            ul, li {
                list-style: none;
            }
    
            .tabBox {
                margin: 10px auto;
                width: 600px;
                overflow: hidden;
            }
    
            .tabBox ul {
                position: relative;
                top: 1px;
                overflow: hidden;
            }
    
            .tabBox ul li {
                /*float: left;*/
                display: inline-block;
                margin-right: 10px;
                width: 80px;
                height: 30px;
                line-height: 30px;
                text-align: center;
                cursor: pointer;
                border: 1px solid green;
            }
    
            .tabBox ul li.bg {
                background: lightblue;
                border-bottom-color: lightblue;
            }
    
            .tabBox div {
                display: none;
                height: 100px;
                line-height: 100px;
                text-align: center;
                border: 1px solid green;
                background: lightblue;
    
            }
    
            .tabBox div.bg {
                display: block;
            }
        </style>
    </head>
    <body>
    <div class="tabBox">
        <ul zhufengFlag="firUl">
            <li class="bg">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
    
        </ul>
        <div class="bg">1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
    </div>
    
    <div class="tabBox">
        <ul zhufengFlag="firUl">
            <li class="bg">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
    
        </ul>
        <div class="bg">1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
    </div>
    
    <script charset="UTF-8" type="text/javascript" src="js/utils.min.js"></script>
    <script type="text/javascript">
        //1、事件委托
        //->利用了事件的冒泡传播机制,如果一个容器A中有很多的后代元素需要绑定点击事件(其他行为统同样可以),这样的话我就不需要一个个获取绑定了,只需要给最外层的容器A绑定一个点击事件即可,这样不管里面那个元素触发了点击的行为,A点击行为对应的方法都会执行,我们在这里通过判断事件源是谁来实现不同的操作即可
        var allTabBox = utils.getElementsByClass("tabBox");
        for (var i = 0; i < allTabBox.length; i++) {
            allTabBox[i].onclick = function (e) {
                e = e || window.event;
                var curTar = e.target || e.srcElement;
    
                //->通过条件确定我们点击的是页卡区域的LI
                if (curTar.tagName.toUpperCase() === "LI" && curTar.parentNode.getAttribute("zhufengFlag") === "firUl") {
    
                    //->让当前点击的这个LI有选中的样式,而其兄弟元素都移除选中样式
                    utils.addClass(curTar, "bg");//当前事件源
                    var curLiSibling = utils.siblings(curTar);
                    for (var k = 0; k < curLiSibling.length; k++) {
                        utils.removeClass(curLiSibling[k], "bg");
                    }
                    //->让和当前点击LI索引对应的那个DIV有选中的样式,其余的DIV移除选中样式
                    var curTarIndex=utils.index(curTar);
                    var divList=utils.nextAll(curTar.parentNode);//->父节点下所有的弟弟
                    for (k = 0; k < divList.length; k++) {
                        k === curTarIndex ? utils.addClass(divList[k], "bg") : utils.removeClass(divList[k], "bg");
                    }
    
                }
            };
        }
    </script>
    </body>
    </html>

    util.js加密后的JS代码:

    var utils=function(){function listToArray(a){var b,c;if(flag)return Array.prototype.slice.call(a,0);for(b=[],c=0;c<a.length;c++)b[b.length]=a[c];return b}function formatJSON(jsonStr){return"JSON"in window?JSON.parse(jsonStr):eval("("+jsonStr+")")}function offset(a){for(var b=a.offsetLeft,c=a.offsetTop,d=a.offsetParent;d;)-1===navigator.userAgent.indexOf("MSIE 8")&&(b+=d.clientLeft,c+=d.clientTop),b+=d.offsetLeft,c+=d.offsetTop,d=d.offsetParent;return{left:b,top:c}}function win(a,b){return"undefined"==typeof b?document.documentElement[a]||document.body[a]:(document.documentElement[a]=b,document.body[a]=b,void 0)}function children(a,b){var d,e,f,g,h,i,c=[];if(flag)c=this.listToArray(a.children);else{for(d=a.childNodes,e=0,f=d.length;f>e;e++)g=d[e],1===g.nodeType?c[c.length]=g:null;d=null}if("string"==typeof b)for(h=0;h<c.length;h++)i=c[h],i.nodeName.toLowerCase()!==b.toLowerCase()&&(c.splice(h,1),h--);return c}function prev(a){if(flag)return a.previousElementSibling;for(var b=a.previousSibling;b&&1!==b.nodeType;)b=b.previousSibling;return b}function next(a){if(flag)return a.nextElementSibling;for(var b=a.nextSibling;b&&1!==b.nodeType;)b=b.nextSibling;return b}function prevAll(a){for(var b=[],c=this.prev(a);c;)b.unshift(c),c=this.prev(c);return b}function nextAll(a){for(var b=[],c=this.next(a);c;)b.push(c),c=this.next(c);return b}function sibling(a){var b=this.prev(a),c=this.next(a),d=[];return b?d.push(b):null,c?d.push(c):null,d}function siblings(a){return this.prevAll(a).concat(this.nextAll(a))}function index(a){return this.prevAll(a).length}function firstChild(a){var b=this.children(a);return b.length>0?b[0]:null}function lastChild(a){var b=this.children(a);return b.length>0?b[b.length-1]:null}function append(a,b){b.appendChild(a)}function prepend(a,b){var c=this.firstChild(b);return c?(b.insertBefore(a,c),void 0):(b.appendChild(a),void 0)}function insertBefore(a,b){b.parentNode.insertBefore(a,b)}function insertAfter(a,b){var c=this.next(b);return c?(b.parentNode.insertBefore(a,c),void 0):(b.parentNode.appendChild(a),void 0)}function hasClass(a,b){var c=new RegExp("(^| +)"+b+"( +|$)");return c.test(a.className)}function addClass(a,b){var d,e,f,c=b.replace(/(^ +| +$)/g,"").split(/ +/g);for(d=0,e=c.length;e>d;d++)f=c[d],this.hasClass(a,f)||(a.className+=" "+f)}function removeClass(a,b){var d,e,f,g,c=b.replace(/(^ +| +$)/g,"").split(/ +/g);for(d=0,e=c.length;e>d;d++)f=c[d],this.hasClass(a,f)&&(g=new RegExp("(^| +)"+f+"( +|$)","g"),a.className=a.className.replace(g," "))}function getElementsByClass(a,b){var c,d,e,f,g,h,i,j,k;if(b=b||document,flag)return this.listToArray(b.getElementsByClassName(a));for(c=[],d=a.replace(/(^ +| +$)/g,"").split(/ +/g),e=b.getElementsByTagName("*"),f=0,g=e.length;g>f;f++){for(h=e[f],i=!0,j=0;j<d.length;j++)if(k=new RegExp("(^| +)"+d[j]+"( +|$)"),!k.test(h.className)){i=!1;break}i&&(c[c.length]=h)}return c}function getCss(a){var b=null,c=null;return flag?b=window.getComputedStyle(this,null)[a]:"opacity"===a?(b=this.currentStyle["filter"],c=/^alpha(opacity=(d+(?:.d+)?))$/,b=c.test(b)?c.exec(b)[1]/100:1):b=this.currentStyle[a],c=/^(-?d+(.d+)?)(px|pt|em|rem)?$/,c.test(b)?parseFloat(b):b}function setCss(a,b){if("float"===a)return this["style"]["cssFloat"]=b,this["style"]["styleFloat"]=b,void 0;if("opacity"===a)return this["style"]["opacity"]=b,this["style"]["filter"]="alpha(opacity="+100*b+")",void 0;var c=/^(width|height|top|bottom|left|right|((margin|padding)(Top|Bottom|Left|Right)?))$/;c.test(a)&&(isNaN(b)||(b+="px")),this["style"][a]=b}function setGroupCss(a){for(var b in a)a.hasOwnProperty(b)&&setCss.call(this,b,a[b])}function css(a){var b=arguments[1],c=Array.prototype.slice.call(arguments,1);if("string"==typeof b){if("undefined"==typeof arguments[2])return getCss.apply(a,c);setCss.apply(a,c)}b=b||0,"[object Object]"===b.toString()&&setGroupCss.apply(a,c)}var flag="getComputedStyle"in window;return{win:win,offset:offset,listToArray:listToArray,formatJSON:formatJSON,children:children,prev:prev,next:next,prevAll:prevAll,nextAll:nextAll,sibling:sibling,siblings:siblings,index:index,firstChild:firstChild,lastChild:lastChild,append:append,prepend:prepend,insertBefore:insertBefore,insertAfter:insertAfter,hasClass:hasClass,addClass:addClass,removeClass:removeClass,getElementsByClass:getElementsByClass,css:css}}();
  • 相关阅读:
    三(奇数)等分两者中间有间隔,两端没间隔
    网易云音乐基于 Flink + Kafka 的实时数仓建设实践
    【电商知识】关于电商定价的几个策略
    硬核!15张图解Redis为什么这么快
    用户画像实践:神策标签生产引擎架构
    数据产品实战(二):ABTest平台
    R代码|基于特征重要性的特征排序代码
    R代码|K均值算法R语言代码
    一文了解R语言数据分析 ----主成分分析
    全网最全 | MySQL EXPLAIN 完全解读
  • 原文地址:https://www.cnblogs.com/kpengfang/p/5447319.html
Copyright © 2011-2022 走看看