jquery实现键盘上下键、return键选折li

注释:code来源javaeye

使用前要引入jquery库和jquery.hotkeys.js ui

HTML CODE

<div class="searchform">
<form action="" method="post" onsubmit="return false;">
<input type="text" name="top_search" class="text global_search" />
<input type="button" value="" class="subform" /><!--<input type="button" value="" class="subform2" />--></form>
<ul id="search_list">
<li><a href="#" title="Private Equity Firms">Private Equity Firms</a></li>
<li><a href="#" title="Porfolio Companies">Porfolio Companies</a></li>
<li><a href="#" title="Professionals">Professionals</a></li>
<li><a href="#" title="Lenders">Lenders</a></li>

</ul>
<input type="hidden" name="prevTrIndex" id="prevTrIndex" value="-1" />
</div>


js code

<script type="text/javascript">
//<![CDATA[
/**
* @about search part js packaging for pe website 3.28
* @Copyright 2011, WANGXIANG
* @MAILTO: flyxiang228@gmail.com
*/
(function ($) {
$.fn.gobalSearch = function (options) {
var defaults = {
contanier: '',
//drop menu contentsearch_list
prevTrIndex: '',
//mark prevTrIndex
items: ['Private Equity Firms', 'Portfolio Companies', 'Professionals', 'Lenders'] //['Private Equity Firms','Portfolio Companies','Professionals','Lenders']
};
var options = $.extend(defaults, options);
this.each(function () {
var globalSearch = $(this);
var searchbtn = globalSearch.next();
var nextele = globalSearch.parent().next();
var drop_li = nextele.children();
var prevEle = $("#" + options.prevTrIndex);
//js方法判断当前搜索框触发return按键
/*globalSearch.keydown(function(event){
var evt=event ? event : (window.event ? window.event:null);//兼容IE和FF
if (evt.keyCode==13){
nextele.show();
globalSearch.blur();
mainDrop();
}
});*/
//jquery借助jquery.hotkey.js插件获取键盘return按键的事件
globalSearch.bind('keydown', 'return', function (evt) {
nextele.show();
globalSearch.blur();
mainDrop();
});
drop_li.bind('click', function () {
var $this = $(this);
var selectVal = $this.text();
var searchVal = globalSearch.val();
var relVal = $this.children('a').attr('rel');
switch (selectVal) {
case options.items[0]:
window.location.href = relVal + searchVal;
break;
case options.items[1]:
window.location.href = relVal + searchVal;
break;
case options.items[2]:
window.location.href = relVal + searchVal;
break;
case options.items[3]:
window.location.href = relVal + searchVal;
break;
default:
return false;
};
nextele.hide();
return false;
});
$(document).bind('keydown', 'up', function (evt) {
var prevTrIndex = parseInt(prevEle.val());
if (prevTrIndex == -1 || prevTrIndex == 0) {
clickTr(trSize - 1);
} else if (prevTrIndex > 0) {
clickTr(prevTrIndex - 1);
};
return false;
}).bind('keydown', 'down', function (evt) {
var prevTrIndex = parseInt(prevEle.val());
if (prevTrIndex == -1 || prevTrIndex == (trSize - 1)) {
clickTr(0);
} else if (prevTrIndex < (trSize - 1)) {
clickTr(prevTrIndex + 1);
};
return false;
}).bind('keydown', 'return', function (evt) {
var prevTrIndex = parseInt(prevEle.val());
var searchVal = globalSearch.val();
var curli = $("#li_" + prevTrIndex);
var selectVal = curli.text();
var relVal = curli.children('a').attr('rel');
switch (selectVal) {
case options.items[0]:
window.location.href = relVal + searchVal;
break;
case options.items[1]:
window.location.href = relVal + searchVal;
break;
case options.items[2]:
window.location.href = relVal + searchVal;
break;
case options.items[3]:
window.location.href = relVal + searchVal;
break;
default:
return false;
};
nextele.hide();
return false;
}).bind('click', function () {
nextele.hide();
});
searchbtn.click(function () {
if (!(nextele.css('display') == 'block')) {
nextele.show();
$(this).blur();
mainDrop();
return false;
} else {
nextele.hide();
return false;
};
}).bind('keydown', 'return', function () {
nextele.show();
$(this).blur();
mainDrop();
return false;
});
//为当前选中的li选中样式


function clickTr(currTrIndex) {
var prevTrIndex = prevEle.val();
if (currTrIndex > -1) {
$("#li_" + currTrIndex).addClass("current");
}
$("#li_" + prevTrIndex).removeClass("current");
prevEle.val(currTrIndex);
$("#nuname").val(currTrIndex);
};
//下拉菜单添加事件集合方法

function mainDrop() {
drop_li.eq(0).focus();
drop_li.removeClass('current');
prevEle.val("-1"); //默认-1
trSize = $("#" + options.contanier).find('li').size(); //li的数量
drop_li.mouseover(function () { //鼠标滑过
$(this).addClass("current");
}).mouseout(function () { //鼠标滑出
$(this).removeClass("current");
}).each(function (i) { //初始化 id 和 index 属性
$(this).attr("id", "li_" + i).attr("index", i);
}).click(function () { //鼠标单击
clickTr($(this).attr("index"));
});
clickTr(0);
return false;
};
});
};
})(jQuery);
//]]-->
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值