模仿select选择框

Css:

.CustomSearch{position:relative;background:#fff; width:170px;}
 .CustomSearch .title{ cursor:pointer; height:20px; line-height:20px;*line-height:22px;overflow:hidden; padding-left:5px; }
.CustomSearch .title em{ float:right; margin:7px 10px 0 0; display:block;width:10px; height:10px; background: url(../images/cssPos/custom_last_bg.jpg) no-repeat -65px -85px;}
.CustomSearch ol{ position:absolute; display:none;width:170px; background:#eee;}
.CustomSearch ol li{ float:none; margin:0; padding-left:5px; cursor:pointer; height:25px; line-height:25px; }
.CustomSearch ol li:hover{ background:#cfcbca; color:#fff;}

DOM:

<div class="CustomSearch">
    <div class="title">
          <em></em>
          <span>选择类别:</span>
          <span class="text">戒指&nbsp;&nbsp;吊坠</span>
          <input type="hidden" value="0" />
    </div>
    <ol class="clearfix">
        <li code="1">戒指</li>
        <li code="2">项链</li>
        <li code="3">手链</li>
        <li code="4">耳钉</li>
    </ol>
</div>

JS:

function CustomSearch(obj){
    this.CustomSearch=$(obj);//保存单个控件
    this.innerText=$(".text",this.CustomSearch);//保存准备改变文本的节点
    this.input=$("input",this.CustomSearch);//保存隐藏域
    this.select=$("ol",this.CustomSearch);//保存下拉框
    this.options=$("li",this.CustomSearch);//保存下拉选项
    this.t=null;
    var _this=this;
    this.CustomSearch.click(function(){
                                _this.select.show();     
                                     }).mouseout(function(){
                                         _this.t=window.setTimeout(function(){
                                                                            _this.select.hide();
                                                                            },100);
                                            
                                         }).mouseover(function(){
                                             window.clearTimeout(_this.t);
                                             });
    this.options.click(function(e){
                                e.stopPropagation();
                                var i=$(this).index();
                                _this.setValue(i);
                                _this.select.hide();
                                });
    };
CustomSearch.prototype={
    setValue:function(i){
        this.innerText.text(this.options.eq(i).text());
        this.input.val(this.options.eq(i).attr("code"));
        }
    
    };
CustomSearch.inite=function(CustomSearchs){
    var _this_=this;
    CustomSearchs.each(function(){
                                new _this_(this);
                                });
    };

调用:

CustomSearch.inite($(".CustomSearch"));

转载于:https://www.cnblogs.com/yangliulang/archive/2012/10/09/2716426.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值