用jQuery模拟select下拉框的简单示例代码

 本篇文章主要是对用jQuery模拟select下拉框的简单示例代码进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助

很多时候,美工会觉得默认的select下拉框很难看(特别是右侧的下拉箭头按钮),他们通常喜欢用一个自定义的图标来代替这个按钮。这样就只能用 js + div 来模拟了,倒腾了一番,用jQuery模拟了下,当然网上这种文章也不少,只是懒得去看找
 代码如下:
<!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=gb2312" />
    <title>自己实现的下拉框</title>
    <style type="text/css" media="all">
        *{font-size:12px;line-height:18px;list-style:none;padding:0;margin:0;text-decoration:none;color:#000;border:0}
        .page{text-align:center;margin:50px;}
        input{border-bottom:solid 1px #ccc;height:18px}              
        .expand{display:none;position:absolute;width:200px;height:100px;overflow-y:auto;border:solid 1px #ccc};
  .expand li{margin:1px 0;background:#fff}
        .expand a{text-decoration:none;display:block;padding:0 5px;background:#efefef;margin:1px 0}
        .expand a:hover{background:#ff9}
    </style>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.min.js"></script>
    <script type="text/javascript">
 
        function showExpand(targetId, expandId, expand_class) {
            //先关掉其它弹出的层
            if (expand_class != undefined) {
                $("." + expand_class).hide();
            }
 
            //判断是否为IE
            var isIE = (! +[1, ]);
 
            var expand = $("#" + expandId);
            var target = $("#" + targetId);
 
            var dx = 0;
            if (isIE) {
                dx = -2;
            }
            else {
                dx = 0;
            }
            expand.get(0).style.left = target.get(0).getBoundingClientRect().left + dx + "px";
            if (isIE) {
                dx = 17;
            }
            else {
                dx = 19;
            }
            expand.get(0).style.top = parseInt(target.get(0).getBoundingClientRect().top) + dx + "px";
            expand.show();
 
            //每个li点击时赋值
            $("#" + expandId).find("li").each(function (i) {
                $(this).show().click(function () {
                    target.val($(this).text().split(' ')[1]);
                    expand.hide();
                })
            })
 
    
        }
  
 
        function search(srcId, expandId) {
            var expand = $("#" + expandId);
            var src = $("#" + srcId);
 
            var A = expand.find("a");
            var v = src.val().toUpperCase();
 
            A.each(function (i) {
                if (v.length >= 2) {
                    if ($(this).text().toUpperCase().indexOf(v) == -1) {
                        $(this).parent().hide();
                    }
                    else {
                        $(this).parent().show();
                    }
                }
                if (v.length <= 0) {
                    $(this).parent().show();
                }
            })
            src.val(v);
        }
 
 
  $().ready(function(){
   $("#txt_city").keyup(function(){
    search('txt_city','city_select1');
   }).focus(function(){
    showExpand('txt_city','city_select1','expand')
   })
 
   $("#txt_city2").keyup(function(){
    search('txt_city2','city_select2');
   }).focus(function(){
    showExpand('txt_city2','city_select2','expand')
   })
  })
 
  function fnTest(){
   document.getElementById("txtTarget").value = document.getElementById("txtSrc").value;
  }
    </script>
</head>
<body>
    <div class="page" style="text-align: center">
        <input type="text" value="" id="txt_city" class="input_expand"  /><a
            href="#" onclick="showExpand('txt_city','city_select1','expand')">▼</a>
        <div class="expand" id="city_select1">
            <ul>
                <li><a href="javascript:void(0)">SH 上海</a></li>
                <li><a href="javascript:void(0)">BJ 北京</a></li>
                <li><a href="javascript:void(0)">HZ 杭州</a></li>
                <li><a href="javascript:void(0)">WH 武汉</a></li>
                <li><a href="javascript:void(0)">NJ 南京</a></li>
                <li><a href="javascript:void(0)">WX 无锡</a></li>
            </ul>
        </div>
 
        <input type="text" value="" id="txt_city2" class="input_expand"  /><a
            href="#" onclick="showExpand('txt_city2','city_select2','expand')">▼</a>
        <div class="expand" id="city_select2">
            <ul>
                <li><a href="javascript:void(0)">CN 中文</a></li>
                <li><a href="javascript:void(0)">EN 英语</a></li>
                <li><a href="javascript:void(0)">JP 日本</a></li>
                <li><a href="javascript:void(0)">RA 俄语</a></li>
                <li><a href="javascript:void(0)">FA 法语</a></li>
                <li><a href="javascript:void(0)">00 其它</a></li>
            </ul>
        </div>
 
  <br/>
  <br/>
  <input type="text" id="txtSrc" onkeyup="fnTest()" />
  <br/>
  <input type="text" id="txtTarget" />
    </div>
</body>
</html>

时间: 2024-08-03 00:42:55

用jQuery模拟select下拉框的简单示例代码的相关文章

用jQuery模拟select下拉框的简单示例代码_jquery

很多时候,美工会觉得默认的select下拉框很难看(特别是右侧的下拉箭头按钮),他们通常喜欢用一个自定义的图标来代替这个按钮.这样就只能用 js + div 来模拟了,倒腾了一番,用jQuery模拟了下,当然网上这种文章也不少,只是懒得去看找 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-t

jQuery模拟select下拉框方法总结

例1,利用div+ul实现实例 因为在IE6下,表单元素select的高度实在不好控制,即使让其和其他元素看起来一样高宽相符,那也仅仅是通过内边距进行的一种视觉误差实现的,而且呢,select控件的滚动条也难看. 正是如此,才会有很多人用div+ul来模拟select下拉框. HTML代码如下,简简单单,一个外div,嵌套一个input和一个ul列表(input用于提交选中的数据):      代码如下 复制代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHT

用jQuery模拟select下拉框

很多时候,美工会觉得默认的select下拉框很难看(特别是右侧的下拉箭头按钮),他们通常喜欢用一个自定义的图标来代替这个按钮.这样就只能用 js + div 来模拟了,倒腾了一番,用jQuery模拟了下,当然网上这种文章也不少,只是懒得去看找,又重新发明轮子鸟:) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-t

jquery实现省市select下拉框的替换(示例代码)_jquery

省市对应的实现:<还有一些没封装起来> 更具选择的省份来确定市的内容 jsp代码: 复制代码 代码如下: <body>  省份<select name="prin">   <option>--请选择--</option>   <option>福建</option>   <option>北京</option>   <option>山东</option>   

jQuery操作select下拉框的text值和value值的方法_jquery

1.jquery获取当前选中select的text值 var checkText=$("#slc1").find("option:selected").text(); 2.jquery获取当前选中select的value值 var checkValue=$("#slc1").val(); 3.jquery获取当前选中select的索引值 var index=$("#slc1 ").get(0).selectedIndex; 4

基于jQuery的select下拉框选择触发事件实例分析_jquery

本文实例讲述了基于jQuery的select下拉框选择触发事件实现方法.分享给大家供大家参考,具体如下: 我一直以来都认为,select 下拉框选择对选项 options 使用 onclick 注册事件即可,如下: <select> <option value="0" onclick="func(0)">选项一</option> <option value="1" onclick="func(1

jquery实现select下拉框美化特效代码分享_javascript技巧

这是一款基于jquery实现select下拉框美化特效代码,用户可以选择下拉菜单内容,是一款非常实用的特效源码.  为大家分享的jquery实现select下拉框美化特效代码如下 <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="css/g.css" type="text/css&

利用jquery获取select下拉框的值_jquery

jquery不是特别熟练,每次使用不常用的就要百度,特地记录下来. 我的下拉框是: <div class="form-group"> <select class="form-control" id="iv_level"> <option value="">店员</option> <option value="">店长</option>

JSuggest自动匹配下拉框使用方法(示例代码)_javascript技巧

1.下载jquery-latest.js,JSuggest.js和JSuggest.css JSuggest.js源代码如下 复制代码 代码如下: /*** * Description : JSuggest 下拉提示框*/ function JSuggest(){ // DIV下拉框this.div = null; // DIV下的ulthis.ul = null; // 文本输入框this.input = null; // 当前DIV所选的LI对象this.current_li = null;