国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

用jQuery模擬select下拉框的簡單示例代碼

2019-11-20 21:12:01
字體:
來源:轉載
供稿:網友

很多時候,美工會覺得默認的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')">

主站蜘蛛池模板: 扬中市| 漾濞| 霍州市| 奉节县| 凤翔县| 浏阳市| 伊金霍洛旗| 教育| 砀山县| 普定县| 定边县| 山西省| 梁山县| 青州市| 宁远县| 麻城市| 鹤山市| 北票市| 榆中县| 沂源县| 汪清县| 武定县| 阜南县| 泰宁县| 双流县| 玛纳斯县| 化州市| 广东省| 宜丰县| 冕宁县| 阿勒泰市| 九龙坡区| 甘德县| 大冶市| 文水县| 天门市| 收藏| 扶余县| 武鸣县| 玛纳斯县| 宿迁市|