css+js美化下拉框(相容IE6、7、8以上主流瀏覽器)

老廖發表於2019-05-11

效果圖:


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>css+js美化下拉框(相容IE6、7、8以上主流瀏覽器)</title>
    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.11.1.min.js"></script>
    <style type="text/css">
        .easyui_combobox {
            width: 300px;
            height: 25px;
            line-height: 25px;
            position: relative;
            border: 1px solid #ccc;
            display: inline-block;
            *display: inline;
            *zoom: 1;
            z-index: 1;
            border-radius: 3px;
        }

            .easyui_combobox span {
                font-size: 13px;
                font-family: '微軟雅黑';
                padding-left: 5px;
            }

            .easyui_combobox i {
                width: 25px;
                height: 100%;
                position: absolute;
                display: inline-block;
                *display: inline;
                *zoom: 1;
                top: 0;
                right: 0;
                border-left: 1px solid #ccc;
                background: #f4f4f4 url(http://vanfun.com/_images/rent_drop_arrow2.png) no-repeat center; /*這裡你自己換成簡頭背景*/
            }

            .easyui_combobox div.list {
                width: 100%;
                line-height: 25px;
                max-height: 250px; /* 25的高 乘 10行 */
                overflow-x: auto;
                position: absolute;
                z-index: 10;
                left: -1px;
                top: 26px;
                border: 1px solid #ccc;
                border-top: 0;
                background: #fff;
                display: none;
                _height: 250px;
                *overflow-x: hidden;
            }

                .easyui_combobox div.list a {
                    height: 25px;
                    width: 100%;
                    display: block;
                    overflow: hidden;
                    text-decoration: none;
                    color: #000;
                    font-size: 13px;
                    font-family: '微軟雅黑';
                    text-indent: 5px;
                }

                    .easyui_combobox div.list a:hover {
                        background: #008b8b;
                        color: #fff;
                    }
    </style>
</head>
<body>
    <div class="easyui_combobox">
        <span>請選擇城市</span>
        <i></i>
        <input type="hidden" name="city" value="" />
        <div class="list">
            <a href="#" data-value="">請選擇城市</a>
            <a href="#" data-value="1">測試內容1</a>
            <a href="#" data-value="2">測試內容2</a>
            <a href="#" data-value="3">測試內容3</a>
            <a href="#" data-value="4">測試內容4</a>
            <a href="#" data-value="5">測試內容5</a>
            <a href="#" data-value="6">測試內容6</a>
            <a href="#" data-value="7">測試內容7</a>
            <a href="#" data-value="8">測試內容8</a>
            <a href="#" data-value="9">測試內容9</a>
            <a href="#" data-value="10">測試內容10</a>
            <a href="#" data-value="11">測試內容11</a>
            <a href="#" data-value="12">測試內容12</a>
        </div>
    </div>
    <div class="easyui_combobox">
        <span>請選擇城市</span>
        <i></i>
        <input type="hidden" name="city" value="" />
        <div class="list">
            <a href="#" data-value="">請選擇城市</a>
            <a href="#" data-value="1">測試內容1</a>
            <a href="#" data-value="2">測試內容2</a>
            <a href="#" data-value="3">測試內容3</a>
            <a href="#" data-value="4">測試內容4</a>
            <a href="#" data-value="5">測試內容5</a>
            <a href="#" data-value="6">測試內容6</a>
            <a href="#" data-value="7">測試內容7</a>
            <a href="#" data-value="8">測試內容8</a>
            <a href="#" data-value="9">測試內容9</a>
            <a href="#" data-value="10">測試內容10</a>
            <a href="#" data-value="11">測試內容11</a>
            <a href="#" data-value="12">測試內容12</a>
        </div>
    </div>
    <div class="easyui_combobox">
        <span>請選擇城市</span>
        <i></i>
        <input type="hidden" name="city" value="" />
        <div class="list">
            <a href="#" data-value="">請選擇城市</a>
            <a href="#" data-value="1">測試內容1</a>
            <a href="#" data-value="2">測試內容2</a>
            <a href="#" data-value="3">測試內容3</a>
            <a href="#" data-value="4">測試內容4</a>
            <a href="#" data-value="5">測試內容5</a>
            <a href="#" data-value="6">測試內容6</a>
            <a href="#" data-value="7">測試內容7</a>
            <a href="#" data-value="8">測試內容8</a>
            <a href="#" data-value="9">測試內容9</a>
            <a href="#" data-value="10">測試內容10</a>
            <a href="#" data-value="11">測試內容11</a>
            <a href="#" data-value="12">測試內容12</a>
        </div>
    </div>
    <br /><br />
    <div class="easyui_combobox">
        <span>請選擇城市</span>
        <i></i>
        <input type="hidden" name="city" value="" />
        <div class="list">
            <a href="#" data-value="">請選擇城市</a>
            <a href="#" data-value="1">測試內容1</a>
            <a href="#" data-value="2">測試內容2</a>
            <a href="#" data-value="3">測試內容3</a>
            <a href="#" data-value="4">測試內容4</a>
            <a href="#" data-value="5">測試內容5</a>
            <a href="#" data-value="6">測試內容6</a>
            <a href="#" data-value="7">測試內容7</a>
            <a href="#" data-value="8">測試內容8</a>
            <a href="#" data-value="9">測試內容9</a>
            <a href="#" data-value="10">測試內容10</a>
            <a href="#" data-value="11">測試內容11</a>
            <a href="#" data-value="12">測試內容12</a>
        </div>
    </div>
    <div class="easyui_combobox">
        <span>請選擇城市</span>
        <i></i>
        <input type="hidden" name="city" value="" />
        <div class="list">
            <a href="#" data-value="">請選擇城市</a>
            <a href="#" data-value="1">測試內容1</a>
            <a href="#" data-value="2">測試內容2</a>
            <a href="#" data-value="3">測試內容3</a>
            <a href="#" data-value="4">測試內容4</a>
            <a href="#" data-value="5">測試內容5</a>
            <a href="#" data-value="6">測試內容6</a>
            <a href="#" data-value="7">測試內容7</a>
            <a href="#" data-value="8">測試內容8</a>
            <a href="#" data-value="9">測試內容9</a>
            <a href="#" data-value="10">測試內容10</a>
            <a href="#" data-value="11">測試內容11</a>
            <a href="#" data-value="12">測試內容12</a>
        </div>
    </div>
    <div class="easyui_combobox">
        <span>請選擇城市</span>
        <i></i>
        <input type="hidden" name="city" value="" />
        <div class="list">
            <a href="#" data-value="">請選擇城市</a>
            <a href="#" data-value="1">測試內容1</a>
            <a href="#" data-value="2">測試內容2</a>
            <a href="#" data-value="3">測試內容3</a>
            <a href="#" data-value="4">測試內容4</a>
            <a href="#" data-value="5">測試內容5</a>
            <a href="#" data-value="6">測試內容6</a>
            <a href="#" data-value="7">測試內容7</a>
            <a href="#" data-value="8">測試內容8</a>
            <a href="#" data-value="9">測試內容9</a>
            <a href="#" data-value="10">測試內容10</a>
            <a href="#" data-value="11">測試內容11</a>
            <a href="#" data-value="12">測試內容12</a>
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
    $(function () {
        /*通用程式碼(裡面的程式碼如果沒BUG或者特殊需求最好不要動)*/
        $(document).on({
            mouseover: function () {
                $(this).css('z-index', 2);
                $(this).children('.list').show();
            },
            mouseout: function () {
                $(this).css('z-index', 1);
                $(this).children('.list').hide();
            }
        }, '.easyui_combobox');

        $(document).on('click', '.easyui_combobox a', function () {
            var $parent = $(this).parents('.easyui_combobox');
            $parent.find('input').val($(this).attr('data-value'));
            $parent.find('span').text($.trim($(this).text()));
            $(this).parent().hide();
            return false;
        });
        /*通用程式碼*/
    });
</script>




請喊我大龍哥最後編輯於:3年前

內容均為作者獨立觀點,不代表八零IT人立場,如涉及侵權,請及時告知。

相關文章