仿QQ列表展开、收缩(1)

这里写图片描述

html:

<ul id="list">
    <li class="lis">
        <h2>我的好友</h2>
        <ul>
            <li>张三</li>
            <li>李四</li>
            ...
        </ul>
    </li>
    <li class="lis">
        <h2>企业好友</h2>
        <ul>
            <li>小明</li>
            <li>小红</li>
            ...
        </ul>
    </li>
    <li class="lis">
        <h2>黑名单</h2>
        <ul>
            <li>哈哈</li>
            ...
        </ul>
    </li>
</ul>

css:

ul,h2 {padding: 0;margin: 0;}
li {list-style: none;}
#list {
    width: 240px;
    border: 1px solid #ccc;
    margin: 0 auto;
}
#list .lis {  }
#list h2 {
    height: 30px;
    line-height: 30px;
    text-indent: 20px;/*文字缩进20px*/
    background: url(img/ico1.gif) no-repeat 5px center pink;/*前面小箭头*/
    cursor: pointer;
}
#list .active { /* 点击时添加的类名 */
    background: url(img/ico2.gif) no-repeat 5px center #ff9;
}
#list ul {display: none;}
#list ul li {
    line-height: 24px;
    border-bottom: 2px solid #fc4;
    text-indent: 24px;/*文字缩进24px*/
}

js:

window.onload = function(){         
    var list = document.getElementById('list');         
    var ah2 = list.getElementsByTagName('h2');          
    var uls = list.getElementsByTagName('ul');

    // h2要和下面的ul进行匹配,所以我们要用索引值,给h2身上添加索引值
    for(var i=0;i<ah2.length;i++){              
        ah2[i].index = i;//给h2添加索引值,点击谁就给谁添加索引值
        ah2[i].onclick = function(){
            if(this.className == ''){//判断所点击的h2标签是否有类,
                //this.index指h2身上的索引数,this指h2,h2的索引值为this.index
                uls[this.index].style.display = 'block'; 
                this.className = 'active';//给当前点击的h2添加类,更改箭头方向
            }else{
                uls[this.index].style.display = 'none';
                this.className = '';
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值