Jquery-based webpage SELECT drop-down box beautification code

Source: Internet
Author: User

1. solved the problem that some select beautification Code cannot trigger the onchange event of the original select control.
2. Multiple calls of $ ("..."). selectCss () are allowed to solve the problem that the Select options cannot be synchronized after update.

The usage is as follows:
Copy codeThe Code is 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 = UTF-8"/>
<Title> untitled document </title>
<Link href = "selectCss.css" rel = "stylesheet" type = "text/css"/>
<Script src = "jquery-1.3.2.min.js" type = "text/javascript"> </script>
<Script src = "selectCss. js" type = "text/javascript"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("Select"). selectCss ();
});
</Script>
</Head>
<Body>
<Select name = "" onchange = "alert (this. value)" id = "select1">
<Option value = "1" title = "option"> option 1 </option>
<Option value = "2"> option 2 </option>
<Option value = "3"> option 3 </option>
</Select>
<Select name = "" id = "select2">
<Option value = "1"> option 31 </option>
<Option value = "2"> option 32 </option>
<Option value = "3"> select 33 </option>
</Select>
</Body>
</Html>

The main files include selectCss.css and selectCss. js.
SelectCss. js file code:
Copy codeThe Code is as follows:
(Function ($ ){
Function hideOptions (speed ){
If (speed. data) {speed = speed. data}
If ($ (document). data ("nowselectoptions "))
{
$ (Document). data ("nowselectoptions"). slideUp (speed );
$ (Document). data ("nowselectoptions"). prev ("div"). removeClass ("tag_select_open ");
$ (Document). data ("nowselectoptions", null );
$ (Document). unbind ("click", hideOptions );
$ (Document). unbind ("keyup", hideOptionsOnEscKey );
}
}
Function hideOptionsOnEscKey (e ){
Var myEvent = e | window. event;
Var keyCode = myEvent. keyCode;
If (keyCode = 27) hideOptions (e. data );
}
Function showOptions (speed ){
$ (Document). bind ("click", speed, hideOptions );
$ (Document). bind ("keyup", speed, hideOptionsOnEscKey );
$ (Document). data ("nowselectoptions"). slideDown (speed );
$ (Document). data ("nowselectoptions"). prev ("div"). addClass ("tag_select_open ");
}

$. Fn. selectCss = function (_ speed ){
$ (This). each (function (){
Var speed = _ speed | "fast ";
If ($ (this). data ("cssobj ")){
$ (This). data ("cssobj"). remove ();
}
$ (This). hide ();
Var divselect = $ ("<div> </div>"). insertAfter (this). addClass ("tag_select ");
$ (This). data ("cssobj", divselect );
Var divoptions = $ ("<ul> </ul>"). insertAfter (divselect). addClass ("tag_options"). hide ();
Divselect. click (function (e ){
If ($ (document). data ("nowselectoptions"). get (0 )! = $ (This). next ("ul"). get (0 )){
HideOptions (speed );
}
If (! $ (This). next ("ul"). is (": visible "))
{
E. stopPropagation ();
$ (Document). data ("nowselectoptions", $ (this). next ("ul "));
ShowOptions (speed );
}
});
Divselect. hover (function (){
$ (This). addClass ("tag_select_hover ");
}
,
Function (){
$ (This). removeClass ("tag_select_hover ");
});
$ (This). change (function (){
$ (This ). nextAll ("ul "). children ("li: eq (" + $ (this) [0]. selectedIndex + ")"). addClass ("open_selected "). siblings (). removeClass ("open_selected ");
$ (This ). next ("div" example .html ($ (this ). children ("option: eq (" + $ (this) [0]. selectedIndex + ")"). text ());
});
$ (This). children ("option"). each (function (I ){
Var lioption = $ ("<li> </li>" example .html ($ (this ). text ()). attr ("title", $ (this ). attr ("title ")). appendTo (divoptions );
If ($ (this). attr ("selected ")){
Lioption. addClass ("open_selected ");
Divselect.html ($ (this). text ());
}
Lioption. data ("option", this );
Lioption. click (function (){
Lioption. data ("option"). selected = true;
$ (Lioption. data ("option"). trigger ("change", true)
});
Lioption. hover (
Function () {$ (this). addClass ("open_hover ");},
Function () {$ (this). removeClass ("open_hover ");}
);
});
});
}
}) (JQuery );

SelectCss. Css file code:
Copy codeThe Code is as follows:
. Tag_select {display: block; color: #000; width: 179px; height: 23px; background: transparent url ("images/index_22.jpg") no-repeat 0 0; padding: 0 10px; line-height: 23px; color: # 7D7D7D; font-size: 12px; cursor: pointer}
. Tag_select_hover {color: # ff0000; background: transparent url ("selectbg.jpg") no-repeat 0 0 ;}
. Tag_select_open {color: # 0000ff; background: transparent url ("selectbg.jpg") no-repeat 0 0 ;}
Ul. tag_options {position: absolute; margin: 0; list-style: none; background: # fff; padding: 0 0 1px; margin: 0; width: 162px; height: 100px; overflow: hidden; overflow-y: auto; font-size: 12px; margin-left: 10px; cursor: pointer; z-index: 1000}
Ul. tag_options li {background: # fff; display: block; width: 140px; padding: 0 10px; height: 20px; text-decoration: none; line-height: 20px; color: #000; font-weight: normal; font-size: 12px}
Ul. tag_options li. open_hover {background: # 0000ff; color: #000; font-weight: normal; font-size: 12px}
Ul. tag_options li. open_selected {background: # ccc; font-size: 12px; font-weight: bold ;}

Selectbg.jpg image:

Related Article

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.