Simple sample code to simulate a select Drop-down box with jquery

Source: Internet
Author: User

  This article is mainly to use the jquery simulation select dropdown box of simple sample code is introduced, need friends can come to the reference, I hope to help you.

Most of the time, the artist will feel that the default Select Drop-down box is hard to see (especially the Drop-down arrow button on the right), and they usually prefer to use a custom icon instead of this button. So you can only use JS + div to simulate, and a little bit, with jquery simulation, of course, online This article also a lot, just lazy to look for   code as follows: <! 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> self-implemented dropdown box </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}               &NBS P       Expand{display:none;position:absolute;width:200px;height:100px;overflow-y:auto;border:solid 1px #ccc};   Expandli{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 ">         & nbsp function Showexpand (Targetid, Expandid, Expand_class) {           //Turn off other pop-up layers   &NBSP ;         if (expand_class!= undefined) {                $ ("." + Expand_class). Hide ();                          //determining whether IE     &nbsp ;       var Isie = (! +[1,]);               var expand = $ ("#" + Expandid);            var target = $ ("#" + Targetid);               var dx = 0;             if (Isie) {                DX =-2; &nb Sp                       else {          & nbsp     DX = 0;                         expand.get (0) Style.left = target.ge T (0). Getboundingclientrect (). Left + dx + "px";             if (Isie) {                DX = &NB Sp                       else {          & nbsp     DX = 19;                         expand.get (0) Style.top = parseint (t Arget.get (0). Getboundingclientrect (). Top)+ dx + "px";             expand.show ();              //per Li Click Assignment             $ ("#" + Expandid). f IND ("Li"). each (function (i) {                $ (this). Show (). Click (function () {& nbsp                   Target.val ($ (this). Text (). Split (") [1]);                     expand.hide ();                 (           })     &NBS p;         }              function search (srcid, Expandid) {             var expand = $ ("#" + Expandid);             var src = $ ("#" + srcid);               var a = Expand.find ("a");         &nbsp   var v = src.val (). toUpperCase ();               A.each (function (i) {              &N Bsp if (v.length >= 2) {                    if ($ (this). Text (). Toupperca Se (). indexOf (v) = = 1) {                        $ (this). Paren T (). hide ();                                   & nbsp     Else {                        $ (this). Parent (). Show ();                                   & nbsp                 if (v.length <= 0) {          &NBSP ;         $ (this). Parent ()-Show ();   &nbsp             {           })         &NBSP ;   Src.val (v);        }       $ (). Ready (function () {   $ ("#txt_city"). KeyUp (function () {&N Bsp   Search (' txt_city ', ' city_select1 ');   &NBSP}). 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:ce Nter ">         <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 Shanghai </a></li>                 <li>& Lt;a href= "javascript:void (0)" >BJ Beijing </a></li>                 < Li><a href= "javascript:void (0)" >hz Hangzhou </a></li>               & nbsp <li><a href= "javascript:void (0)" >wh Wuhan </a></li>                 <li><a href= "javascript:void (0)" >nj Nanjing </a></li>                 <li><a href= "JavaScript:void (0) ">wx Wuxi </a></li>             </ul>       & nbsp </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 ">         &NBS P   <ul>                 <li><a href= "javascript:void (0)" >CN Chinese </a></li>                 <li><a href= "javascript:void (0) ">en English </a></li>               <li><a href=" javascript: void (0) ">JP Japan </a></li>                 <li><a href=" Java Script:void (0) ">ra Russian</a></li>                 <li><a href= "javascript:void (0)" & GT;FA French </a></li>                 <li><a href= "Javascript:vo ID (0) ">00 other </a></li>             </ul>         ;/div>     <br/>   <br/>   <input type= "text" id= "txtsrc" onkeyup= "fntest ()"/> nbsp <br/>   <input type= "text" id= "Txttarget"/>     </div> </body> </html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.