Implement up/down key switch control on the keyboard in the ecshop search box using javascript
In the createSelect () function, an object is returned. 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.