dom 按着shift多选

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>

<style>
body { margin: 0; padding: 0; }
ul { padding: 0; margin: 0; }
li { list-style: none; }
a { text-decoration: none; color: #000; display: block; }
#div1 { width: 250px; height: 20px; border: 1px solid #fe6601; padding: 5px; line-height:20px; margin: 20px auto 0 auto;}
#ul1 { margin: 0 auto; width: 260px; line-height: 20px; border: 1px solid #fe6601; display: none;}
#ul1 li a { padding: 5px; }
#ul1 li a:hover { background: #fe6601; color: #fff; }
</style>
<script type="text/javascript">
window.onload = function ()
{
    var odiv = document.getElementById('div1');
    var oul = document.getElementById('ul1');
    var arr = [];
    
    odiv.onclick = function (ev)
    {
        var ev = ev || event;
        ev.cancelBubble = true;
        oul.style.display = 'block';
    }
    
    for( var i = 0; i < oul.children.length; i++)
    {
        oul.children[i].onclick = function (ev)
        {
            var ev = ev || event;
            if(ev.shiftKey)
            {
                ev.cancelBubble = 'true';
            }
            
            for(var i = 0; i < arr.length; i++)
            {
                if(this.children[0].innerHTML == arr[i])
                {
                    arr.splice(i,1);
                    odiv.innerHTML = arr.join(',');
                    bgcolor();
                    return;
                }
            }
            arr.push ( this.children[0].innerHTML);
            odiv.innerHTML = arr.join(',');
            bgcolor();
            
            function bgcolor ()
            {
                for(var i = 0; i < oul.children.length; i++)
                {
                    oul.children[i].children[0].style.background = '';
                    oul.children[i].children[0].style.color = '';
                }
                
                var arr1 = odiv.innerHTML.split(',');
                for(var i = 0; i < arr1.length; i++)
                {
                    for(var j = 0; j < oul.children.length ; j++)
                    {
                        if(oul.children[j].children[0].innerHTML == arr1[i])
                        {
                            oul.children[j].children[0].style.background = '#fe6601';
                            oul.children[j].children[0].style.color = '#fff';
                        }
                    }
                }
            }
        }
    }
    
    document.onclick = function ()
    {
        oul.style.display = 'none';
        arr = [];
    }
};
</script>
</head>

<body>
    <div id="div1">按住shift多</div>
    <ul id="ul1">
        <li><a href="javascript:;">宋体</a></li>
        <li><a href="javascript:;">黑体</a></li>
        <li><a href="javascript:;">楷体</a></li>
        <li><a href="javascript:;">微软雅黑</a></li>
        <li><a href="javascript:;">新宋体</a></li>
        <li><a href="javascript:;">仿宋</a></li>
    </ul>
</body>
</html>

 

转载于:https://www.cnblogs.com/mayufo/p/4229155.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值