zoukankan      html  css  js  c++  java
  • 模仿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"));
  • 相关阅读:
    js中(function(){…})()立即执行函数写法理解
    JS 立即执行的函数表达式(function)写法
    javascript中call,apply,bind的用法对比分析
    C++成员函数指针的应用
    typeid详解
    dynamic_cast
    C++标准转换运算符dynamic_cast
    继承的构造函数
    考虑写一个不抛出异常的swap函数
    布隆过滤器(转)
  • 原文地址:https://www.cnblogs.com/yangliulang/p/2716426.html
Copyright © 2011-2022 走看看