某大千模拟原生js secelt 利用ul的index属性快速实现

1.

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script type="text/javascript">
    </script>
    <style>
    * {
        margin: 0;
        padding: 0;
    }
    
    ul,
    li {
        list-style: none;
    }
    
    .secelt {
        overflow: hidden;
        width: 382px;
        margin: 0 auto;
    }
    
    .secelt h2 {
        color: #fff;
        box-shadow: 0 0 3px #000;
        background-color: #827272;
        text-align: center;
        font-size: 16px;
        line-height: 40px;
    }
    
    .secelt .seceltCon {
        float: left;
        width: 120px;
    }
    
    .secelt ul {
        display: none;
    }
    
    .secelt .seceltCon+.seceltCon {
        margin-left: 10px;
    }
    
    .secelt li {
        width: 120px;
        line-height: 39px;
        text-align: center;
        color: #fff;
        background-color: #ccc;
        border-bottom: 1px solid;
    }
    
    .secelt li.active {
        background-color: green;
    }
    </style>
</head>
<body>
<div class="secelt">
    <div class="seceltCon">
        <h2>湖南-桂东</h2>
        <ul>
            <li>湖南-桂东</li>
            <li>湖南-炎陵</li>
            <li>湖南-株洲</li>
        </ul>
    </div>
    <div class="seceltCon">
        <h2>上海-宝山</h2>
        <ul>
            <li>上海-宝山</li>
            <li>上海-浦东</li>
            <li>上海-虹桥</li>
        </ul>
    </div>
    <div class="seceltCon">
        <h2>北京-天安门</h2>
        <ul>
            <li>北京-天安门</li>
            <li>北京-三里盹</li>
            <li>北京-故宫</li>
        </ul>
    </div>
</div>
<script type="text/javascript">
init();

function init() {
    var secelt = document.getElementsByClassName('secelt')[0];
    var seceltCon = secelt.getElementsByClassName('seceltCon')
    var ul = secelt.getElementsByTagName('ul')

    var h2 = secelt.getElementsByTagName('h2')
    var showUl = null;

    for(var i = 0; i < seceltCon.length; i++) {
        seceltCon[i].setAttribute('index', i)
        seceltCon[i].onclick = function(e) {

            if(showUl) {
                showUl.style.display = 'none'
            }
            ul[this.getAttribute('index')].style.display = 'block'
            showUl = ul[this.getAttribute('index')]

            var ev = e || window.event;
            ev.stopPropagation()
        }
    }

    document.onclick = function(e) {
        if(showUl) {
            showUl.style.display = 'none'
        }
    }

    for(var i = 0; i < ul.length; i++) {
        (function(ul) {
            var ali = ul.getElementsByTagName('li');
            for(var m = 0; m < ali.length; m++) {
                ali[m].onmouseover = function() {
                    this.className = 'active'
                }
                ali[m].onmouseout = function() {
                    this.className = ''
                }
                ali[m].onclick = function(e) {

                    h2[this.parentNode.parentNode.getAttribute('index')].innerText = this.innerText;
                    this.parentNode.style.display = 'none';

                    var ev = e || window.event;
                    ev.stopPropagation()
                }
            }

        })(ul[i])
    }

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

 

posted @ 2018-01-30 14:51  small-match  阅读(181)  评论(0)    收藏  举报