下拉选框

body

<div id="box" >
            <div id="title">请选择游戏名称</div>
            <ul id='list' style="list-style: none;border-top: none;">
                <li>王者荣耀</li>
                <li>英雄联盟</li>
                <li>天涯明月刀</li>
                <li>梦幻西游</li>
                <li>刺激战场</li>
                <li>DNF</li>
            </ul>
        </div>

样式

<style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            body{
                background: gray;
            }
            #box{
                width: 150px;
                margin: 0 auto;
                text-align: center;
                /*border: 1px solid black;*/            
            }
            #title{
                height: 50px;
                line-height: 50px;
                border: 1px solid black;            
                
            }
            #list{
                background-color: black;
                color: white;
                border: 1px solid black;
                display: none;
            }
            #box ul li{
                height: 50px;
                line-height: 50px;
                border-bottom: 1px dashed white;
            }
        </style>

JS写法

<script type="text/javascript">
            onload = function(){
                title.onmouseenter = list.onmouseenter = function(){
                    list.style.display = 'block'
                }
                
                title.onmouseleave = list.onmouseleave = function(){
                    list.style.display = 'none'
                }
                
                
                var olist = document.getElementsByTagName('li')
                for(var i=0;i<olist.length;i++){
                    olist[i].onmouseenter = function(){
                        this.style.backgroundColor = 'gray'
                    }
                    
                    olist[i].onmouseleave = function(){
                        this.style.backgroundColor = 'black'
                    }
                    
                    olist[i].onclick = function(){
                        title.innerHTML = this.innerHTML
                        list.style.display = 'none'
                    }
                }
                
                
            }
        </script>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容