• 用js实现菜单的下拉列表,实用又简单


      下拉列表本可以用<select>配合<option>来写,方便得很。但是在前端中,好用的东西都有兼容,为了避免出现兼容性的问题,下拉列表用js写再合适不行了。

    <body>部分————————简单的布个局

    <body>
        <div id="box">请选择手机名称</div>
        <div id="down">
            <ul class="phones">
                <li>华为</li>
                <li>小米</li>
                <li>oppo</li>
                <li>vivo</li>
                <li>爱疯</li>
                <li>三星</li>
            </ul>
        </div>
    </body>

    <style>部分————————再简单也要把样式写好看点

    <style>
            #box{
                color: aliceblue;
                 110px;
                height: 25px;
                border: 1px solid #c5c5c5;
                border-radius: 10px;
                background-color: #797777;
                text-align: center;
                /* text-indent: 5px; */
                font-size: 14px;
                line-height: 25px;
                cursor: pointer;
            }
            #down{
                border: 1px solid #c5c5c5;
                 90px;
                margin-left: 5px;
                display: none;
            }
            ul{
                padding: 0;
                margin: 0;
            }
            li{
                list-style: none;
                font-size: 14px;
                border-bottom: 1px dashed #c5c5c5;
                text-align: center;
                height: 25px;
                line-height: 25px;
                color: aliceblue;
                background-color: #333;
                cursor: pointer;
            }
            li:hover{
                background-color: #5c0e0e;
            }
        </style>

    <script>部分————————实现功能的部分

    <script>
            var obox = document.getElementById("box");
            var odown = document.getElementById("down");
            var oli = document.querySelectorAll("li");
            console.log(oli);
            var timer;
            //当点击obox时,呈现出下拉列表的内容,给个延时效果
            obox.onclick = function(){
                clearInterval(timer);
                timer = setInterval(function(){
                    odown.style.display = "block";
                },300)
                ///选中列表中的某一项并将其呈现在box中,隐藏下拉列表
                for(var i=0;i<oli.length;i++){
                    oli[i].n = i;
                    oli[i].onclick = function(){
                        obox.innerHTML = this.innerHTML;
                        odown.style.display = "none";
                        clearInterval(timer);
                    }
                }
            }
    </script>

      以上就是我写的一个简单的下拉列表,尚有欠缺处,还望包含。

  • 相关阅读:
    HttpClient使用
    十九、springboot使用@ControllerAdvice(二)之深入理解
    如何同步删除svn管理的package包目录
    在使用FastJson开发遇到的的坑
    解决tomcat端口被占用:Port 8005 required by Tomcat v7.0 Server at localhost is already in use
    SpringBoot使用Mybatis-Generator
    SpringCloud Gateway入门
    使用Nginx部署静态网站
    SpringBoot使用Jsp
    SpringBoot应用War包形式部署到外部Tomcat
  • 原文地址:https://www.cnblogs.com/funseey/p/11416120.html
Copyright © 2020-2023  润新知