使用jquery做类似搜索引擎效果

  1. var line = 0;
  2. var sendType;
  3. function del(){
  4. if($("#newDiv")){
  5. $("#newDiv").remove();
  6. line = 0;
  7. }
  8. }
  9. $(document).ready(function(){
  10. //文本框失去焦点时层消失
  11. $(document.body).click(function(){
  12. del();
  13. });
  14. $(document).keydown(function(){
  15. // 38 上 40下 13 回车
  16. if($("#newDiv")){
  17. if(event.keyCode == 40){
  18. $("table tbody tr").eq(line)
  19. .css("backgroundColor", "blue")
  20. .css("color", "white");
  21. $("table tbody tr").eq(line < 0 ? 0 : line - 1)
  22. .css("backgroundColor", "white")
  23. .css("color", "black");
  24. line = (line == $("table tbody tr").length ? 0 : line + 1);
  25. }else if(event.keyCode == 38){
  26. line = (line == 0 ? $("table tbody tr").length : line - 1);
  27. $("table tbody tr").eq(line)
  28. .css("backgroundColor", "blue")
  29. .css("color", "white");
  30. $("table tbody tr").eq(line > $("table tbody tr").length ? 0 : line + 1)
  31. .css("backgroundColor", "white")
  32. .css("color", "black");
  33. }else if(event.keyCode == 13){
  34. $("#phoneno").val($("table tbody tr").eq(line - 1).find("td").eq(0).html());
  35. del();
  36. }
  37. }
  38. });

  39. //只要id为phoneno的文本框变化,就调用function()

  40. $("#phoneno").bind("propertychange", function(){
  41. del();
  42. var top = $("#phoneno").offset().top;
  43. var left = $("#phoneno").offset().left;
  44. var newDiv = $("<div/>").width($("#phoneno").width() + 6)
  45. .css("position", "absolute")
  46. .css("backgroundColor", "white")
  47. .css("left", left)
  48. .css("top", top + $("#phoneno").height() + 6)
  49. .css("border", "1px solid blue")
  50. .attr("id", "newDiv");

  51. var table = $("<table width='100%'/>")
  52. .attr("cellpadding", "0")
  53. .attr("cellspacing", "0");
  54. //$.get("xmlPhoneno", {phoneno: $("#phoneno").val()},function(xml){
  55. $.get("phoneNoInfo.xml", {PhoneNumber: $("#phoneno").val()},function(xml){

  56. //这里调用了后台的xmlController来执行发挥xml。以下为解析xml
  57. $(xml).find("phones phone").each(function(){
  58. var phoneno = $(this).attr("phoneno");
  59. var sname = $(this).attr("sname");
  60. var tr = $("<tr/>").css("cursor", "pointer").mouseout(function(){
  61. $(this).css("backgroundColor", "white").css("color", "black");
  62. }).mouseover(function(){
  63. $(this).css("backgroundColor", "blue").css("color", "white");
  64. }).click(function(){
  65. $("#phoneno").val($(this).find("td").eq(0).html());
  66. del();
  67. });
  68. var td1 = $("<td/>").html(phoneno).css("fontSize", "12px")
  69. .css("margin", "5 5 5 5");
  70. var td2 = $("<td/>").html(sname)
  71. .attr("align", "right").css("fontSize", "12px")
  72. .css("color", "green").css("margin", "5 5 5 5");

  73. tr.append(td1).append(td2);
  74. table.append(tr);
  75. newDiv.append(table);
  76. });
  77. });
  78. $(document.body).append(newDiv);
  79. if($("#phoneno").val() == ""){
  80. $("#newDiv").remove();
  81. }
  82. });
  83. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值