jQuery模拟Select下拉框

本文介绍如何利用jQuery实现自定义Select下拉框样式,解决浏览器间样式不一致的问题,提供HTML、CSS及jQuery代码实现步骤。

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

转自:http://www.zjgsq.com/1268.html

select几乎我们都会碰到,而默认的select下拉框很难看。而且各个浏览器的SELECT下拉框的默认样式都不一样,可能在CHROME里很美观,但是一到IE就丑爆了。这里我们用jQuery来模拟Select下拉框。

jQuery-select

jQuery-select

HTML代码

01
02
03
04
05
06
07
08
09
10
11
< div class = "selectContainer"
             < span class = "selectOption gray" >点击选择下拉框内容</ span
             < ul class = "selectMenu"
                 < li >江西省</ li
                 < li >广东省</ li
                 < li >江苏省</ li
                 < li >河北省</ li
                 < li >湖南省</ li
             </ ul
             < span class = "shows" ></ span
  </ div >

CSS代码

01
02
03
04
05
06
07
08
09
10
11
12
13
/* ---- Select ----*/ 
.selectContainer{ position : relative width : 262px display :inline- block _display : inline ; _zoom: 1 z-index : 1000 background : #FFF border : 1px solid #CCC
.selectContainer input{} 
.selectContainer .selectOption{ min-width : 180px padding : 0 5px line-height : 25px height : 25px white-space : nowrap overflow : hidden ; border : none width : 250px z-index : 1000
.selectContainer .shows{ width : 20px height : 20px position : absolute right : 2px top : 2px background : url (images/select-ico.gif)  no-repeat center center
.selectContainer ul{ position : absolute width : 100% top : 25px left : -1px border-bottom : 1px solid #CCC display : none ;} 
.selectContainer ul li{ padding : 0 5px border : 1px solid #CCC border-bottom : 1px solid #EEE border-top : none line-height : 25px width : 252px background : #FFF cursor : pointer
.selectContainer ul li:hover{ background : #F5F5F5
 
.selectContainer ul.dis{ display : block !important ;} 
.selectContainer ul.undis{ display :noneimportant;} 
.zIndex{ z-index : 10000 !important
.selectContainer . gray { color : #DDD }

jQuery代码

使用前记得引入jQuery

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
( function ($){ 
 
     jQuery.fn.select =  function (options){ 
         return this .each( function (){ 
             var $ this = $( this ); 
             var $shows = $ this .find( ".shows" ); 
             var $selectOption = $ this .find( ".selectOption" ); 
             var $el = $ this .find( "ul &gt; li" ); 
 
             $ this .click( function (e){ 
                 $( this ).toggleClass( "zIndex" ); 
                 $( this ).children( "ul" ).toggleClass( "dis" ); 
                 e.stopPropagation(); 
             }); 
 
             $el.bind( "click" , function (){ 
                 var $this_ = $( this ); 
 
                 $ this .find( "span" ).removeClass( "gray" ); 
                 $this_.parent().parent().find( ".selectOption" ).text($this_.text()); 
             }); 
 
             $( "body" ).bind( "click" , function (){ 
                 $ this .removeClass( "zIndex" ); 
                 $ this .find( "ul" ).removeClass( "dis" );     
             }) 
 
         //eahc End   
         }); 
 
    
 
})(jQuery); 
 
$( ".selectContainer" ).select();   //调用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值