h5实现下拉选择

该文章展示了一个使用HTML、CSS和jQuery编写的简单下拉菜单示例。菜单在点击后通过slideToggle方法显示和隐藏,点击子菜单项会更新父菜单的文字内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、先看效果图 

 

 

二、简单实现一下代码

首先你要引入jQuery.js文件

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>下拉菜单</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        text-decoration: none;
        list-style: none;
    }

    .downorder {
        width: 88px;
        height: 32px;
        font-size: 14px;
        border: 1px solid #eee;
        border-radius: 4px;
        position: relative;
        margin: 100px;
    }

    .order {
        height: 100%;
        color: #666;
        display: flex;
        justify-content: left;
        align-items: center;
        padding-left: 10px;
    }

    .order::after {
        content: '';
        width: 20px;
        height: 20px;
        position: absolute;
        right: 5px;
        display: inline-block;
        background: url(../../img/select1.png) no-repeat center/100% 100%;
    }

    .downtext {
        border-radius: 4px;
        overflow: hidden;
        border: 1px solid #eee;
        background-color: #fff;
        position: absolute;
        top: 32px;
        left: 0;
        z-index: 10;
        /* display: none; */
    }

    .downtext a {
        width: 100%;
        height: 32px;
        display: inline-block;
        font-size: 14px;
        line-height: 30px;
        text-align: center;
        color: #666;
    }
</style>

<body>
    <!--下拉菜单-->
    <div class="downorder">
        <p class="order">
            下拉菜单
        </p>
        <div class="downtext">
            <a href="#">text1</a>
            <a href="#">text2</a>
            <a href="#">text3</a>
            <a href="#">text4</a>
        </div>
    </div>
    <script src="../../js/jquery-3.2.1.min.js"></script>
    <script>
        // 下拉菜单
        $(function () {
            $(".downtext").hide();
            $(".order").click(function () {
                $(".downtext").slideToggle();
            })
            $(".downtext a").click(function () {
                var index = $('.downtext a').index(this);
                var a = document.querySelectorAll(".downtext a");
                $(".order").text(a[index].innerHTML);
                $(".downtext").hide();
            })
        })
    </script>
</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值