下面给出js代码
var line = 0;//定义一个全局的line以用于记录当前显示的是哪一行
function replace(obj,src,str){//用str替换掉源(obj)中的src
if(obj.indexOf(src)==-1)return obj;
var array = obj.split(src);
var result="";
for(var i=0;i<array.length;i++){
result +=array;
result+=str;
}
return result;
}
var mychange = function () {//进行ajax请求以及显示提示信息
del();//如果存在提示框则移除
if ($("#keywords").val() == "")return;
var top = $("#keywords").offset().top;//搜索框与页面顶部的距离
var left = $("#keywords").offset().left;//搜索框与页面左侧的距离
//构造信息提示的div
var newDiv = $("<div/>").width($("#keywords").width() + 10).css("position", "absolute").css("backgroundColor", "white").css("left", left).css("top", top + $("#keywords").height() + 10).css("border", "1px solid #DFDFDF").attr("id", "newDiv");
var table = $("<table width='100%'/>").attr("cellpadding", "0").attr("cellspacing", "0");
$.post(basePath+"autocompleteServlet.select", {key:$("#keywords").val()}, function (xml) {//发送请求,并通过xml的格式返回处理信息
$(xml).find("results result").each(function () {//获取处理信息中所有的result节点
var key = $(this).attr("key");//result中的key属性
var sug = $(this).attr("sug");//result中的sug属性,记录提示信息
var count = $(this).attr("count");//result中的count属性,记录通过该关键字的最后一次查询所得的总数
var tr = $("<tr/>").css("cursor", "pointer").mouseout(function () {//鼠标移到提示信息上时的样式变换
$(this).css("backgroundColor", "white").css("color", "black");
}).mouseover(function () {//鼠标移出提示信息时的样式变换
$(this).css("backgroundColor", "#DFDFDF").css("color", "white");
}).click(function () {//点击提示信息
var html = $(this).find("td").eq(0).html();//获取提示信息中的有效值
//剔除提示信息中的<B><b>标签
html = replace(html,"<B>","");
html = replace(html,"</B>","");
html = replace(html,"<b>","");//firefox
html = replace(html,"</b>","");//firfox
$("#keywords").val(html);//将提示信息赋给搜索框
del();//移除提示框
$("#searchform").submit();//表单提交
});
//提示信息中的有效值,将非关键字部分加粗显示
var td1 = $("<td/>").html(key+"<b>"+sug+"</b>").css("fontSize", "14px").css("color", "black").css("margin", "5 5 5 5").css("padding","0 5px");
var td2 = $("<td/>").html("约"+count+"条结果").attr("align", "right").css("fontSize", "13px").css("color", "green").css("margin", "5 5 5 5");
tr.append(td1).append(td2);
table.append(tr);
newDiv.append(table);
});
if($(xml).find("results result").length==0){//没有提示结果时
del();//移除提示框
}
});
$(document.body).append(newDiv);//显示提示框
if ($("#keywords").val() == "") {
$("#newDiv").remove();
}
}
function del() {//如果存在提示框则移除
if ($("#newDiv")) {
$("#newDiv").remove();
line = 0;
}
}
var keywords = "";//用于记录用户输入的关键字,因为在上下键操作的过程中会改变搜索框的值,用户在通过键盘上下键操作时应该还可以返回到他最初的状态
$(document).ready(function () {
//提示框失去焦点时层消失
$(document.body).click(function () {
del();
});
$(document).keyup(function(event){//用户通过键盘进行操作,按键弹起时
$("#keywords").bind("propertychange", mychange);//将mychange事件绑定到propertychange事件上
$("#keywords").bind("input", mychange);//firefox 与上面的效果类似,只是在firefox中是input事件
if(event.keyCode != 40 && event.keyCode != 38 && event.keyCode != 13){//当用户不是在按上下与回车键时,将搜索框中的值赋给keywords,记录用户的初始状态
keywords = $("#keywords").val();
}
});
$(document).keydown(function (event) { // 38 上 40下 13 回车
if ($("#newDiv")) {//当提示框存在时
if (event.keyCode == 40) {//down
$("#keywords").unbind("propertychange");//移除propertychange事件
$("#keywords").unbind("input");//firefox 与上面类似
var html = "";
if(line==$("#newDiv table tbody tr").length){//如果line值为当前提示信息行数的值时,将用户最初输入的信息赋给搜索框
html = keywords;
$("#newDiv table tbody tr").eq($("#newDiv table tbody tr").length-1).css("backgroundColor", "white").css("color", "black");//消除选中样式
}else{//当有提示信息行选中时
$("#newDiv table tbody tr").eq(line).css("backgroundColor", "#DFDFDF").css("color", "white");//改变样式
html = $("#newDiv table tbody tr").eq(line).find("td").eq(0).html();//获取有效提示信息
html = replace(html,"<B>","");
html = replace(html,"</B>","");
html = replace(html,"<b>","");//firefox
html = replace(html,"</b>","");//firfox
if( $("#newDiv table tbody tr").length != 1){//改变样式
$("#newDiv table tbody tr").eq(line < 0 ? 0 : line - 1).css("backgroundColor", "white").css("color", "black");
}
}
line = (line == $("#newDiv table tbody tr").length ? 0 : line + 1);//line值不为提示信息行数的值时line值加1,否则line归0
$("#keywords").val(html);//将结果赋给搜索框
} else {
if (event.keyCode == 38) {//up
$("#keywords").unbind("propertychange");
$("#keywords").unbind("input");//firefox
var html = "";
line = (line == 0 ? $("#newDiv table tbody tr").length : line-1 );
if(line == 0){
html = keywords;
$("#newDiv table tbody tr").eq(0).css("backgroundColor", "white").css("color", "black");
}else{
$("#newDiv table tbody tr").eq(line-1).css("backgroundColor", "#DFDFDF").css("color", "white");
$("#newDiv table tbody tr").eq(line > $("table tbody tr").length? 0 : line ).css("backgroundColor", "white").css("color", "black");
html = $("#newDiv table tbody tr").eq(line-1).find("td").eq(0).html();
html = replace(html,"<B>","");
html = replace(html,"</B>","");
html = replace(html,"<b>","");//firefox
html = replace(html,"</b>","");//firfox
}
$("#keywords").val(html);
} else {
if (event.keyCode == 13) {//enter
if($("#newDiv table tbody tr")&& $("#newDiv table tbody tr").length>0){//有提示信息时
var html = "";
if(line==0){//当line=0时
if($("#newDiv table tbody tr").eq(0).css("backgroundColor")=="#DFDFDF"){//第一行被选中
html =$("#newDiv table tbody tr").eq(0).find("td").eq(0).html();
}else{//没有进行选择时
return;
}
}else{
html = $("#newDiv table tbody tr").eq(line - 1).find("td").eq(0).html();
}
html = replace(html,"<B>","");
html = replace(html,"</B>","");
html = replace(html,"<b>","");//firefox
html = replace(html,"</b>","");//firfox
$("#keywords").unbind("propertychange");//移除propertychange事件
$("#keywords").unbind("input");//firefox 与上面的操作类似
$("#keywords").val(html);
}
del();//移除提示框
}
}
}
}
});
$("#keywords").bind("propertychange", mychange);//ie 将mychange绑定到propertychange事件上
$("#keywords").bind("input", mychange);//firefox 与上面类似
});
|