设为首页 加入收藏

TOP

js 导航栏多项点击显示下拉菜单代码(一)
2019-09-17 19:00:51 】 浏览:71
Tags:导航 多项 点击 显示 下拉 菜单 代码
<!DOCTYPE html>
<html>
<head>
    <title>Dropdown</title>
    <!--<link rel="stylesheet" href="style.css">-->
    <!--<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>-->
    <!--<script src="js/jquery.min.js"></script>-->
    <meta charset="utf-8" />
    <style type="text/css">
        body {
            font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
        }
        ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
            overflow: hidden;
            background-color: #333;
        }
        li {
            float: left;
        }
        li a, .dropbtn {
            display: inline-block;
            color: white;
            text-align: center;
            padding: 14px 16px;
            text-decoration: none;
        }
        li a:hover, .dropdown:hover .dropbtn {
            background-color: #1f75cf;
        }
        li.dropdown {
            display: inline-block;
        }
        .dropdown-content {
            display: none;
            position: absolute;
            background-color: #fafafa;
            min-width: 160px;
            box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
        }
        .dropdown-content a {
            color: black;
            padding: 12px 16px;
            text-decoration: none;
            display: block;
            text-align: left;
        }
        .dropdown-content a:hover {
            color: white;
            background-color: #1f75cf;
        }
        .show {
            display: block;
        }
    </style>
</head>
<body>
<ul>
    <li class="dropdown">
        <a id="a" href="java script:void(0)" class="dropbtn" onclick="showList(this)">标题 A</a>
        <div class="dropdown-content" id="dropdown-a">
            <a href="#">下拉 1</a>
            <a href="#">下拉 2</a>
            <a href="#">下拉 3</a>
        </div>
    </li>
    <li class="dropdown">
        <a id="b" href="java script:void(0)" class="dropbtn" onclick="showList(this)">标题 B</a>
        <div class="dropdown-content" id="dropdown-b">
            <a href="#">下拉 1</a>
            <a href="#">下拉 2</a>
            <a href="#">下拉 3</a>
        </div>
    </li>
</ul>
<script type="text/java script">
    function showList(o) {
        //1.把其他的二级ul都隐藏,
//        hideList("dropdown-content" + o.id);
        //把其他的二级ul隐藏方式1
        hideList("dropdown-" + o.id);
        //把其他的二级ul隐藏方式2
//        hideList('');


        //2.再切换自己的show。

//        document.getElementById("dropdown-" + o.id).classList.toggle("show");
        var drop = document.getElementById("dropdown-" + o.id);
        if (drop.classList.contains('show')) {
            drop.classList.remove('show');
//            alert("yichu");
        }else{
            drop.classList.add('show');
//            alert("xianshi");
        }
    }

    //隐藏不是自己对应的下拉div。
    function hideList(option) {
        var dropdowns = document.getElementsByClassName("dropdown-content");

        for (var i = 0; i < dropdowns.length; i++) {
            var openDropdown = dropdowns[i];
            if (openDropdown.id != option) {
                if (openDropdown.classList.contains('show')) {
                    openDropdown.classList.remove('show');
                }
            }
        }
    }
   


    windo
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇类似淘宝五星点评 ,Jquery实现代.. 下一篇【微信小程序】开发实战 之 「数..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目