下面是我以前用到的键盘的js文件: /** * 传入所有的<tr>行的总数目,和滚动条能滚动的总高度 * 每一个屏幕能显示N行数据。每间隔N行,就翻动屏幕,滚屏 * */ var setScollTop = function(nowNum, id) { var totalHeight = (window.Height - $("#search").height() - $("#head").height()*2) / 2; // alert("totalHeight=" + totalHeight); var rowHeight = $("#body>tr:first-child").height(); // alert("rowHeight=" + rowHeight); var rate = parseInt(totalHeight / rowHeight) - 2; var times = parseInt(nowNum / rate); var scrollTop = rowHeight * times * rate; // alert(scrollTop); // $("#" + id).scrollTop(scrollTop); $("#" + id).animate({scrollTop: scrollTop}, 200); } /** * 禁止通过上下键操作滚动条 * */ var forbid = function() { if (window.addEventListener) { document.addEventListener("keydown", function(e) { var keyCode = e.keyCode; if (keyCode >= 37 && keyCode <= 40) { e.preventDefault(); } }); } else if (window.attachEvent) { document.attachEvent("onkeydown", function(e) { var keyCode = e.keyCode; if (keyCode >= 37 && keyCode <= 40) { e.returnValue = false; } }); } } /** * 用户在按下左右键的时候,页面左右移动 * @return */ var horizontalMove = function() { document.onkeydown = function(e){ forbid(); var e = e || event; var ele = event.srcElement || event.target; var id = ele.id; if (ele.tagName != "TABLE") { //alert('no match'); id = ele.parentElement.parentElement.parentElement.id; } if (id == "up") { //用户选择的版块页面 var nowIndex = parseInt($("#up .select_bg").attr("class").substr(2)); var maxIndex = parseInt($("#up .select_bg").parent().children("tr:last-child").attr("class").substr(2)); } else if (id == "down") { var nowIndex = parseInt($("#down .select_bg").attr("class").substr(2)); var maxIndex = parseInt($("#down .select_bg").parent().children("tr:last-child").attr("class").substr(2)); } var currKey = e.keyCode || e.which || e.charCode; if (currKey == 37) { //左键 if (id == "up") { document.getElementById("first").scrollLeft -= 50; } else { document.getElementById("second").scrollLeft -= 50; } } else if (currKey == 39) { //右方向键 if (id == "up") { document.getElementById("first").scrollLeft += 50; } else { document.getElementById("second").scrollLeft += 50; } } else if (currKey == 38) { //上方向键 var preIndex = nowIndex - 1; //上一行的索引值 if (preIndex < 0) return; if (id == "up") { //用户选择的版块页面 $("#up .bk" + preIndex).addClass("select_bg"); $("#up .bk" + nowIndex).removeClass("select_bg"); window.select1 = $("#up .bk" + preIndex).attr("id"); bkInteraction(window.select1); setScollTop(preIndex, "first"); } else if(id == "down"){ //用户选择的个股页面 $("#down .gg" + preIndex).addClass("select_bg"); $("#down .gg" + nowIndex).removeClass("select_bg"); window.select2 = $("#down .gg" + preIndex).attr("id"); ggInteraction(window.select2); setScollTop(preIndex, "second"); } } else if (currKey == 40) { //下方向键 var nextIndex = nowIndex + 1; //上一行的索引值 if (nextIndex > maxIndex) return; if (id == "up") { //用户选择的版块页面 $("#up .bk" + nextIndex).addClass("select_bg"); $("#up .bk" + nowIndex).removeClass("select_bg"); window.select1 = $("#up .bk" + nextIndex).attr("id"); bkInteraction(window.select1); setScollTop(nextIndex, "first"); // $("#up").focus(); } else if(id == "down"){ //用户选择的个股页面 $("#down .gg" + nextIndex).addClass("select_bg"); $("#down .gg" + nowIndex).removeClass("select_bg"); window.select2 = $(".gg" + nextIndex).attr("id"); ggInteraction(window.select2); setScollTop(nextIndex, "second"); // $("#down").focus(); } } else if (currKey == 13) { //用户输入的是回车 if (id == "up") { //用户选择的版块页面 var nowIndex = "bk" + parseInt($("#up .select_bg").attr("class").substr(2)); } else if(id == "down"){ //用户选择的个股页面 var nowIndex = "gg" + parseInt($("#down .select_bg").attr("class").substr(2)); } var selectId = $("." + nowIndex).attr("id"); var quote = external.createObject("Quote"); var obj = {}; obj.id = 379; obj.code = selectId; obj.period = 68040; quote.switchPage(obj); } e.stopPropagation(); } }