JavaScript之QQ列表

JavaScript之QQ列表

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
<style type="text/css">
    ul,h2 {
        padding: 0;
        margin: 0;
    }
    li {
        list-style: none;
    }
    #list {
        width: 240px;
        border: 1px solid #333;
        margin: 0 auto;
    }
    #list .lis{

    } 
    #list h2 {
        height: 30px;
        line-height: 30px;
        text-indent: 20px;
        background: url(../picture/menu1.png) no-repeat 5px center blue;
        color: #fff;
    }
    #list .active {
        background: url(../picture/menu2.png) no-repeat 5px center #132db0;
        color: #fff;
    }
    #list ul{
        display: none;
    }
    #list ul li {
        line-height: 24px;
        border-bottom: 1px solid #333;
        text-indent: 24px;
    }
    #list ul .hover {
        background: #8ea1fe;
    }
</style>
<script type="text/javascript">
    window.onload = function(){
        var oUL = document.getElementById('list');
        var aH2 = oUL.getElementsByTagName('h2');
        var aUL = oUL.getElementsByTagName('ul');
        var aLi = null;
        var arrLi =[];

        for(var i=0;i<aH2.length;i++){
            aH2[i].index = i;
            aH2[i].onclick = function(){

                if (this.className == '') {
                    aUL[this.index].style.display = 'block';
                this.className = 'active';
            } 
                else {
                    aUL[this.index].style.display = 'none';
                    this.className = '';
                }
            }
        }
        for (var i=0;i<aUL.length;i++){
            aLi = aUL[i].getElementsByTagName('li');
            for (var j=0;j<aLi.length;j++){
                arrLi.push(aLi[j]);
            }
        }
        for(var i=0;i<arrLi.length;i++){
            arrLi[i].onclick = function(){
                for(var i=0;i<arrLi.length;i++){
                    arrLi[i].className='';
                }
                this.className='hover';
            }
        }
    }
</script>
</head>
<body>
    <ul id="list">
        <li class="lis">
            <h2>人物介绍1</h2>
            <ul>
                <li>人物1</li>
                <li>人物2</li>
                <li>人物3</li>
                <li>人物4</li>
                <li>人物5</li>
            </ul>
        </li>
        <li class="lis">
            <h2>人物介绍2</h2>
            <ul>
                <li>人物1</li>
                <li>人物2</li>
                <li>人物3</li>
                <li>人物4</li>
                <li>人物5</li>
            </ul>
        </li>
        <li class="lis">
            <h2>人物介绍3</h2>
            <ul>
                <li>人物1</li>
                <li>人物2</li>
                <li>人物3</li>
                <li>人物4</li>
                <li>人物5</li>
            </ul>
        </li>
    </ul>
</body>
</html>

这里写图片描述
只能选中一个人物

arrLi.push(aLi[j])

将aLi[j]中的所有的元素放到arrLi的数组中

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值