加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

DWR实现模拟Google搜索效果

发布时间:2020-12-15 22:10:08 所属栏目:百科 来源:网络整理
导读:[html] view plain copy !--模拟google搜索-- script type = "text/javascript" /********************************可配置选项********************************/ //被选中的相似关键字背景颜色 var selectedBgColor = "#CCCCCC" ; //未被选中的相似关键字背
[html] view plain copy
  1. <!--模拟google搜索-->
  2. <scripttype="text/javascript">
  3. /********************************可配置选项********************************/
  4. //被选中的相似关键字背景颜色
  5. varselectedBgColor="#CCCCCC";
  6. //未被选中的相似关键字背景颜色
  7. varunselectedBgColor="#FFFFFF";
  8. //相似关键字列表框的边框
  9. varlistBorder="1solid#000000";
  10. /***************************************************************************/
  11. /********************************不可配置选项********************************/
  12. //上一次输入的关键字(用来判断关键字是否有改变,没有则不再去服务端重新获取提示关键字)
  13. varoldKeyValue;
  14. //鼠标相对于提示关键字列表框的位置(0:提示框外面,1:提示框里面)
  15. varmouseLocation=0;
  16. //当前选中的提示关键字索引(从0开始,等于-1表示没有被选中项)
  17. varselectedKeyIndex=-1;
  18. //上一次选中的提示关键字索引(从0开始,等于-1表示没有上次被选中项)
  19. varoldSelectedKeyIndex=-1;
  20. //提示关键字总数
  21. vartdCount=0;
  22. /***************************************************************************/
  23. /*
  24. 用途:给String对象添加去除左右空格方法
  25. */
  26. String.prototype.trim=function(){
  27. varm=this.match(/^s*(S+(s+S+)*)s*$/);
  28. return(m==null)?"":m[1];
  29. }
  30. 用途:初始化提示关键字列表框的状态
  31. functioninitKeyListState(){
  32. selectedKeyIndex=-1;
  33. oldSelectedKeyIndex=-1;
  34. tdCount=0;
  35. }
  36. /*
  37. 用途:将上一次选中的关键字项变为未选中
  38. */
  39. functiondisSelectedOldKey(){
  40. //判断说明:oldSelectedKeyIndex!=selectedKeyIndex
  41. //当只有一个相似关键字的时候,则不存在上一次选中和这次选中关键字,
  42. //只要第一次选中后,按向上或向下箭头都是选中。
  43. if(oldSelectedKeyIndex!=-1&&oldSelectedKeyIndex!=selectedKeyIndex){
  44. $('keyId'+oldSelectedKeyIndex).bgColor=unselectedBgColor;
  45. //上一次选中项更新
  46. oldSelectedKeyIndex=selectedKeyIndex;
  47. 用途:当按上下箭头时选中新的提示关键字项,按回车时将选中的提示关键字输入到搜索框。
  48. functionsetSelectedKey(){
  49. //$('keyId0')存在表示有相关提示关键字,不存在则不处理。
  50. if($('keyId0')){
  51. if(event.keyCode==38){
  52. //------处理向上事件------
  53. if(selectedKeyIndex==-1){
  54. selectedKeyIndex=tdCount-1;
  55. }else{
  56. selectedKeyIndex=(selectedKeyIndex+tdCount-1)%tdCount;
  57. $('keyId'+selectedKeyIndex).bgColor=selectedBgColor;
  58. disSelectedOldKey();
  59. }elseif(event.keyCode==40){
  60. //------处理向下事件------
  61. if(selectedKeyIndex==-1){
  62. selectedKeyIndex=0;
  63. }else{
  64. selectedKeyIndex=(selectedKeyIndex+1)%tdCount;
  65. $('keyId'+selectedKeyIndex).bgColor=selectedBgColor;
  66. disSelectedOldKey();
  67. }elseif(event.keyCode==13){
  68. //------处理回车事件------
  69. $('cond').value=$('keyId'+selectedKeyIndex).innerText;
  70. setCursorLast($('cond'));
  71. //隐藏提示关键字列表框
  72. $('dropdownlistDiv').style.display='none';
  73. 用途:获取相似关键字
  74. functiongetConformKey(){
  75. //得到输入的关键字
  76. varkeyValue=$('cond').value.trim();
  77. //如果这次的查询关键字和上次的一样,则不去服务器重新获取相似关键字列表。
  78. if(keyValue!=oldKeyValue){
  79. //关键字为空则不去服务器获取相似关键字列表
  80. if(keyValue==''){
  81. DWRUtil.removeAllRows('showKeyList');
  82. setDropListVisible(false);
  83. initKeyListState();
  84. //采用ajax异步模式获取相似关键字(这里的useraction,改成自己的action)
  85. useraction.findByLike(keyValue,conformKeyCallback);
  86. 用途:获取关键字回调方法
  87. functionconformKeyCallback(keyList){
  88. initKeyListState();
  89. if(keyList.length>0){
  90. //生成相似关键字提示框
  91. DWRUtil.addRows('showKeyList',keyList,cellFuncs,{
  92. cellCreator:function(options){
  93. vartd=document.createElement("td");
  94. td.id='keyId'+tdCount++;
  95. td.onmouSEOver=function(){selectedKeyIndex=parseInt(this.id.substring(5,td.id.length));this.bgColor=selectedBgColor;disSelectedOldKey();};
  96. td.onclick=function(){
  97. $('cond').value=this.innerText;
  98. $('cond').focus();
  99. setCursorLast($('cond'));
  100. };
  101. returntd;
  102. },escapeHtml:false});
  103. setDropListVisible(true);
  104. 用途:表格数据显示处理方法
  105. varcellFuncs=[
  106. function(data){returndata.username;}
  107. ];
  108. 用途:将输入框的光标移到最后
  109. functionsetCursorLast(inputObj){
  110. varinputRange=inputObj.createTextRange();
  111. inputRange.collapse(true);
  112. inputRange.moveStart('character',inputObj.value.length);
  113. inputRange.select();
  114. 用途:创建相似关键字列表框
  115. functioncreateShowDiv(){
  116. varshowDiv=document.createElement("div");
  117. showDiv.id="dropdownlistDiv";
  118. with(showDiv.style){
  119. position="absolute";
  120. //层的绝对位置从这调整
  121. left=parseInt($('cond').style.left.replace('px',''))+190;
  122. top=parseInt($('cond').style.top.replace('px',''))+parseInt($('cond').style.height.replace('px',''))+28;
  123. width=parseInt($('cond').style.width.replace('px',''));
  124. border=listBorder;
  125. zIndex="1";
  126. display='none';
  127. backgroundColor=unselectedBgColor;
  128. showDiv.onmouSEOver=function(){mouseLocation=1;};
  129. showDiv.onmouSEOut=function(){mouseLocation=0;};
  130. //overflow设置滚动条
  131. showDiv.innerHTML="<divstyle='width:100%;height:150px;overflow:auto;'><tableborder='0'style='width:100%;height:20%;font-size:12;color:#33CC00;'><tbodyid='showKeyList'style='margin-left:0;margin-right:0;margin-bottom:0;margin-top:0;'></tbody></table></div>";
  132. document.body.appendChild(showDiv);
  133. 用途:设置相似关键字列表框是否可见
  134. 参数:isDisplay,true表示可见,false表示不可见
  135. functionsetDropListVisible(isDisplay){
  136. if(mouseLocation==1){
  137. return;
  138. if(($('cond').value.trim()!='')&&(isDisplay==true)){
  139. $('dropdownlistDiv').style.display='';
  140. else{
  141. //将创建相似关键字列表框方法附加到onload事件中
  142. if(window.addEventListener){
  143. window.addEventListener('load',createShowDiv,false);
  144. }elseif(window.attachEvent){
  145. window.attachEvent('onload',createShowDiv);
  146. </script>

这个js可以放在你需要实现搜索效果的jsp里,或单独保存为js文件都可以.

copy
    divstyle="position:absolute;left:190px;top:25px;">
  1. inputAUTOCOMPLETE="off"
  2. onkeydown="oldKeyValue=this.value.trim();setSelectedKey();"
  3. onkeyup="getConformKey();"
  4. onfocus="if(this.value=='找人')this.value='';setDropListVisible(true);"
  5. onblur="setDropListVisible(false);"
  6. style="width:300;height:23;z-index:10;top:0;left:0;"type="text"name="cond"value="找人"id="cond"/>
  7. inputtype="button"class="btn"value="搜一下"onclick="findBylike();"/>
  8. div>

useraction.findByLike(String name);是dao层的一个查询方法,
返回一个List,把这里换成你自己的实现就可以了.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读