使用div模拟select下拉菜单

摘抄自:https://www.cnblogs.com/ranran/p/div_select_css.html


代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
    .mod_select{position:absolute;left:30%;top:100px;font-familY:Arial, Helvetica, sans-serif;}
    .mod_select ul{margin:0;padding:0;}
    .mod_select ul li{list-style-type:none;float:left;margin-left:20px;height:24px;}
    .select_label{color:#982F4D;float:left;line-height:24px;padding-right:10px;font-size:12px;font-weight:700;}
    .select_box{float:left;border:solid 1px #EDE7D6;color:#444;position:relative;cursor:pointer;width:165px;background:url(../select_bg.jpg) repeat-x;font-size:12px;}
    .selet_open{display:inline-block;border-left:solid 1px #E5E5E5;position:absolute;right:0;top:0;width:30px;height:24px;background:url(../select_up.jpg) no-repeat center center;}
    .select_txt{display:inline-block;padding-left:10px;width:135px;line-height:24px;height:24px;cursor:text;overflow:hidden;}
    .option{width:165px;;border:solid 1px #EDE7D6;position:absolute;top:24px;left:-1px;z-index:2;overflow:hidden;display:none;}
    .option a{display:block;height:26px;line-height:26px;text-align:left;padding:0 10px;width:100%;background:#fff;}
    .option a:hover{background:#FDE0E5;}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function(){
            $(".select_box").click(function(event){   
                event.stopPropagation();
                $(this).find(".option").toggle();
                $(this).parent().siblings().find(".option").hide();
            });
            $(document).click(function(event){
                var eo=$(event.target);
                if($(".select_box").is(":visible") && eo.attr("class")!="option" && !eo.parent(".option").length)
                $('.option').hide();                                      
            });
            /*赋值给文本框*/
            $(".option a").click(function(){
                var value=$(this).text();
                $(this).parent().siblings(".select_txt").text(value);
                $("#select_value").val(value)
             })
        })
</script>
</head>
               
<body>
<div class="mod_select">
    <ul>
        <li>
            <span class="select_label">sort buy:</span>
            <div class="select_box">
                <span class="select_txt"></span><a class="selet_open"><b></b></a>
                <div class="option">
                    <a>1</a>
                    <a>2</a>
                    <a>3</a>
                </div>
            </div>
            <br clear="all" />
        </li>
        <li>
            <span class="select_label">View:</span>
            <div class="select_box">
                <span class="select_txt"></span><a class="selet_open"></a>
                <div class="option">
                    <a>1</a>
                    <a>2</a>
                    <a>3</a>
                </div>
            </div>
        </li>
    </ul>
    <input type="hidden" id="select_value" />
</div>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这里给出一个简单的实现使用了jQuery和CSS来模拟下拉菜单效果。 HTML代码: ``` <div class="custom-select"> <select> <option value="">请选择</option> <option value="1">选项一</option> <option value="2">选项二</option> <option value="3">选项三</option> </select> </div> ``` CSS代码: ``` .custom-select { position: relative; display: inline-block; font-size: 16px; font-weight: 500; line-height: 1.5; color: #333; } .custom-select select { display: none; } .custom-select::after { position: absolute; top: 50%; right: 10px; transform: translateY(-50%); content: "\f107"; font-family: "FontAwesome"; font-size: 14px; color: #999; pointer-events: none; } .custom-select.open::after { content: "\f106"; color: #666; } .custom-select-dropdown { position: absolute; top: 100%; left: 0; right: 0; z-index: 1000; display: none; max-height: 240px; overflow-y: auto; background-color: #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2); } .custom-select.open .custom-select-dropdown { display: block; } .custom-select-dropdown-option { padding: 8px 12px; font-size: 14px; line-height: 1.5; color: #333; cursor: pointer; } .custom-select-dropdown-option:hover { background-color: #f5f5f5; } ``` JavaScript代码: ``` $(document).on("click", function(e) { var $target = $(e.target); if ($target.closest(".custom-select").length) { $target.closest(".custom-select").toggleClass("open"); } else { $(".custom-select").removeClass("open"); } }); $(".custom-select select").on("change", function() { var text = $(this).find("option:selected").text(); $(this).closest(".custom-select").find(".custom-select-text").text(text); }); $(".custom-select select").each(function() { var $this = $(this), numberOfOptions = $(this).children("option").length; $this.addClass("s-hidden"); $this.wrap('<div class="custom-select"></div>'); $this.after('<div class="custom-select-text">' + $this.children("option").eq(0).text() + '</div>'); var $dropdown = $('<div class="custom-select-dropdown"></div>'); for (var i = 0; i < numberOfOptions; i++) { $dropdown.append('<div class="custom-select-dropdown-option">' + $this.children("option").eq(i).text() + '</div>'); } $this.after($dropdown); }); ``` 这段代码的实现原理是: 1. 隐藏原生的下拉菜单,并在其外部包裹一个自定义的容器。 2. 在容器中添加一个显示当前选项的文本框,以及一个下拉菜单容器。 3. 遍历所有选项,把它们添加到下拉菜单容器中。 4. 绑定点击事件,让点击自定义下拉菜单的容器时,切换下拉菜单的显示状态。 5. 绑定下拉菜单选项的点击事件,让其选中对应的选项,并更新文本框的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值