Javascript implements up/down key switch control on the keyboard in the ecshop search box _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to implement the upper/lower key switch control on the keyboard of the ecshop search box in javascript. If you need it, refer to the createSelect () function to return an object. The two methods of this object are next ()
And the moveSelect () called in prev () can point to this function correctly, or
The moveSelect () function is put out.

The Code is as follows:


/* Recommended keyboard operations and problems */
Var curDo = null;
Var select = createSelect ();
$ ('# Keyword'). keyup (function (e ){
Var theEvent = e | window. event;
Code = theEvent. keyCode? TheEvent. keyCode: (theEvent. which? TheEvent. which: theEvent. charCode)
Var KEY = {
UP: 38,
DOWN: 40,
DEL: 46,
TAB: 9,
RETURN: 13,
ESC: 27,
BACKSPACE: 8,
LEFT: 37,
RIGHT: 39
};
ClearTimeout (curDo); // when the keyboard pops up, cancel the timed ajax Data Acquisition operation.
Switch (code ){
Case KEY. UP:
Select. next ();
Break;
Case KEY. DOWN:
Select. prev ();
Break;
Case KEY. RETURN:
$ ('. Suggest-hover'). trigger ('click ');
Break;
Case KEY. LEFT:
Break;
Case KEY. RIGHT:
Break;
Default:
CurDo = setTimeout (getSuggest (), 300 );
Break;
}
});
/* Suggest Select Operation */
Function createSelect (){
Var CLASSES = {
ACTIVE: "suggest-hover"
};
Function moveSelect (step ){
Var listItems = $ ('. suggest-results li ');
// Number of current hover steps
Var active;
Active = $ ('.' + CLASSES. ACTIVE). index ();
ListItems. eq (active). removeClass (CLASSES. ACTIVE );
Active + = step;
If (active <0 ){
Active = listItems. size ()-1;
} Else if (active> = listItems. size ()){
Active = 0;
}
Var activeItem = listItems. eq (active). addClass (CLASSES. ACTIVE );
};
Return {
Next: function (){
MoveSelect (-1 );
},
Prev: function (){
MoveSelect (1 );
}
};
};

The above is all the content shared in this article. I hope you will like it.

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.