轻松使用div模拟select下拉菜单

没有办法,平时不是万不得已我是不喜欢去模拟各类控件的,一个是麻烦,二个是对性能也有些影响,还是原生的来的实在。
老板昨天发话,必须模拟赶紧的,老外最喜欢简洁干净的风格,说的貌似都很在理的样子,业务部也是一样,都是一些不懂产品的人来提需求哈哈,不说啦。
功能也不难就是多耗了我一点点时间,本来是不用做的,任务早就做完了。为了满足老板提的需求我还是改了,尊重她的选择,毕竟人家是老板,这叫“做事不依东,累死都无功”,在读我文章的朋友们你要牢记了。

下面直接看我代码。

<!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>

我昨天测试了的:IE6——IE9,火狐,谷歌都木有问题,放心的用吧。
如果发现那里有问题还请亲给我留言哦。

原文地址:https://www.cnblogs.com/ranran/p/div_select_css.html