<! 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> SimulateUpDownKeySelect.html </title> <Style type = "text/css"> # DivSelect {border: 1px solid red; width: 208px! Important; width: 210px ;} # DivSelect ul {width: 200px; margin: 3px; margin-left:-35px; * margin-left: 3px; overflow: hidden} # DivSelect ul li {float: left; list-style-type: none; width: 45px; height: 14px; line-height: 20px; font: 14px arial; text-align: center; padding: 2px} # DivSelect li: hover {background: green; cursor: pointer} # TxtInput {width: 205px ;} </Style> </Head> <Body> <Form method = "post" action = "#"> <Input type = "text" id = "txtInput" value = "" autocomplete = "off" onkeydown = "if (event. keyCode = 13) return false;"/> <! -- Prevents the Enter key from triggering form submission OnKeyPress --> <Div id = "divSelect"> </Div> <Script type = "text/javascript"> Var list = "<ul>" List + = "<li> science fiction film </li> <li> war film </li> <li> action film </li> <li> Love Film </li> <li> drama film </li> <li> documentary </li> <li> variety shows </li> <li> comedy </li> <li> cartoons </li> <li> inspirational films </li> <li> horror films </li> <li> costumes </li> <li> TV series </li> <li> reading </li> <li> novels </li> <li> portfolio </li> <li> history </li> <li> poetry </li> <li> prose </li> <li> military </li> "; List + = "</ul>" Document. getElementById ('divselect'). innerHTML = list; </Script> </Form> <Script type = "text/javascript"> <! -- Function $ (sId) { Return document. getElementById (sId ); } Function clearSelectedOptBgColor (target) { If (target. seletedIndex> = 0) Target. options [target. seletedIndex]. style. background = ""; } Function setSelectedOptBgColor (target) { Target. options [target. seletedIndex]. style. background = "green "; } Var upKeyCode = 38; Var downKeyCode = 40; Var enterKeyCode = 13; Var oInput = $ ("txtInput "); OInput. options = $ ("divSelect"). getElementsByTagName ("li "); OInput. seletedIndex =-1; OInput. focus (); // OInput. onKeyPress {} OInput. onkeyup = function (event ){ If (event = undefined) Event = window. event; Switch (event. keyCode) { Case 37: ClearSelectedOptBgColor (this ); This. seletedIndex --; If (this. seletedIndex <0) This. seletedIndex = this. options. length-1; This. value = this. options [this. seletedIndex]. innerHTML; SetSelectedOptBgColor (this ); Break; Case 38: ClearSelectedOptBgColor (this ); This. seletedIndex = this. seletedIndex-4; If (this. seletedIndex <0) This. seletedIndex = this. options. length-1; This. value = this. options [this. seletedIndex]. innerHTML; SetSelectedOptBgColor (this ); Break; Case 39: ClearSelectedOptBgColor (this ); This. seletedIndex ++; If (this. seletedIndex> = this. options. length) This. seletedIndex = 0; This. value = this. options [this. seletedIndex]. innerHTML; SetSelectedOptBgColor (this ); Break; Case 40: ClearSelectedOptBgColor (this ); This. seletedIndex = this. seletedIndex + 4; If (this. seletedIndex> = this. options. length) This. seletedIndex = 0; This. value = this. options [this. seletedIndex]. innerHTML; SetSelectedOptBgColor (this ); Break; Case enterKeyCode: This. value = this. options [this. seletedIndex]. innerHTML; // Alert ('A ') Break; } }; OInput. onblur = function (){ ClearSelectedOptBgColor (this ); This. seletedIndex = 1; }; // --> </Script> </Body> </Html> |