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.