<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    我的漫漫程序之旅

    專注于JavaWeb開發
    隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
    數據加載中……

    DWR實現模擬Google搜索效果

    <!-- 模擬google搜索 -->
    <script type="text/javascript">
      
    /********************************可配置選項********************************/
        
    // 被選中的相似關鍵字背景顏色
        var selectedBgColor = "#CCCCCC";
        
    // 未被選中的相似關鍵字背景顏色
        var unselectedBgColor = "#FFFFFF";
        
    // 相似關鍵字列表框的邊框
        var listBorder = "1 solid #000000"
        
    /***************************************************************************/ 
     
        
    /********************************不可配置選項********************************/
        
        
    // 上一次輸入的關鍵字(用來判斷關鍵字是否有改變,沒有則不再去服務端重新獲取提示關鍵字)
        var oldKeyValue;
        
    // 鼠標相對于提示關鍵字列表框的位置(0:提示框外面,1:提示框里面)
        var mouseLocation = 0;
        
    // 當前選中的提示關鍵字索引(從0開始,等于-1表示沒有被選中項)
        var selectedKeyIndex = -1
        
    // 上一次選中的提示關鍵字索引(從0開始,等于-1表示沒有上次被選中項)
        var oldSelectedKeyIndex = -1;
        
    // 提示關鍵字總數
        var tdCount = 0;
        
        
    /***************************************************************************/
        
          
    /*
            用途:給String對象添加去除左右空格方法
        
    */
     
        String.prototype.trim 
    = function() {
          
    var m = this.match(/^\s*(\S+(\s+\S+)*)\s*$/);
          
    return (m == null? "" : m[1];
        }

        
        
    /*
            用途:初始化提示關鍵字列表框的狀態
        
    */
     
        
    function initKeyListState(){
            selectedKeyIndex 
    = -1
            oldSelectedKeyIndex 
    = -1;
            tdCount 
    = 0;
        }

        
        
    /*
            用途:將上一次選中的關鍵字項變為未選中
        
    */
     
        
    function disSelectedOldKey(){
              
    //判斷說明:oldSelectedKeyIndex!=selectedKeyIndex
              //        當只有一個相似關鍵字的時候,則不存在上一次選中和這次選中關鍵字,
              //        只要第一次選中后,按向上或向下箭頭都是選中。
            if (oldSelectedKeyIndex!=-1&&oldSelectedKeyIndex!=selectedKeyIndex){
                  $('keyId'
    + oldSelectedKeyIndex).bgColor=unselectedBgColor;
              }
        
              
    // 上一次選中項更新
              oldSelectedKeyIndex = selectedKeyIndex;
        }

        
        
    /*
            用途:當按上下箭頭時選中新的提示關鍵字項,按回車時將選中的提示關鍵字輸入到搜索框。
        
    */
     
        
    function setSelectedKey(){
            
    // $('keyId0')存在表示有相關提示關鍵字,不存在則不處理。
            if($('keyId0'))
              
    if (event.keyCode==38){
                
    //------處理向上事件------
                  if (selectedKeyIndex==-1){
                      selectedKeyIndex 
    = tdCount-1;
                  }
    else{
                      selectedKeyIndex
    = (selectedKeyIndex+tdCount-1)%tdCount;
                  }

                  $('keyId'
    + selectedKeyIndex).bgColor= selectedBgColor;    
                  disSelectedOldKey();
              }
    else if (event.keyCode==40){
                
    //------處理向下事件------
                  if (selectedKeyIndex==-1){
                      selectedKeyIndex 
    = 0;
                  }
    else{
                      selectedKeyIndex 
    = (selectedKeyIndex+1)%tdCount;
                  }

                  $('keyId'
    + selectedKeyIndex).bgColor= selectedBgColor;
                  disSelectedOldKey();    
              }
    else if (event.keyCode==13){
                
    //------處理回車事件------
                  $('cond').value=$('keyId'+ selectedKeyIndex).innerText;
                  setCursorLast($('cond'));
                  
    // 隱藏提示關鍵字列表框
                  $('dropdownlistDiv').style.display='none';
              }

          }

        }

        
        
    /*
            用途:獲取相似關鍵字
        
    */
     
        
    function getConformKey(){
          
    //得到輸入的關鍵字
          var keyValue = $('cond').value.trim();
          
    // 如果這次的查詢關鍵字和上次的一樣,則不去服務器重新獲取相似關鍵字列表。
          if (keyValue!=oldKeyValue){
              
    // 關鍵字為空則不去服務器獲取相似關鍵字列表
              if (keyValue==''){
                  DWRUtil.removeAllRows('showKeyList');
                  setDropListVisible(
    false);
                  initKeyListState();
              }
    else{
                  
    //采用ajax異步模式獲取相似關鍵字(這里的useraction,改成自己的action)
                  useraction.findByLike(keyValue,conformKeyCallback);
              }

          }

        }

        
        
    /*
            用途:獲取關鍵字回調方法
        
    */
     
        
    function conformKeyCallback(keyList){
            DWRUtil.removeAllRows('showKeyList');
            initKeyListState();
            
    if (keyList.length>0){
                
    // 生成相似關鍵字提示框
                DWRUtil.addRows('showKeyList',keyList,cellFuncs, 
                    cellCreator:
    function(options) {
                      
    var td = document.createElement("td");
                      td.id 
    = 'keyId' + tdCount++;
                      td.onmouseover 
    = function (){selectedKeyIndex=parseInt(this.id.substring(5,td.id.length));this.bgColor=selectedBgColor;disSelectedOldKey();};
                      td.onclick
    = function (){
                          $('cond').value
    =this.innerText;
                          $('cond').focus();
                        setCursorLast($('cond'));
                        $('dropdownlistDiv').style.display
    ='none';
                      }
    ;
                      
    return td;
                    }
    ,escapeHtml:false}
    );
                setDropListVisible(
    true);
            }
    else{
                setDropListVisible(
    false);
            }

        }

        
        
    /*
            用途:表格數據顯示處理方法
        
    */
     
        
    var cellFuncs = [
          
    function(data) return data.username; }
        ];
        
        
    /*
            用途:將輸入框的光標移到最后
        
    */
     
        
    function setCursorLast(inputObj){
            
    var inputRange = inputObj.createTextRange();  
            inputRange.collapse(
    true);
            inputRange.moveStart('character',inputObj.value.length); 
            inputRange.select();
        }

                
        
    /*
            用途:創建相似關鍵字列表框
        
    */
     
        
    function createShowDiv(){
            
    var showDiv = document.createElement("div");
            showDiv.id 
    = "dropdownlistDiv";   
            
    with(showDiv.style){   
               position 
    = "absolute"
               
    //層的絕對位置從這調整  
               left = parseInt($('cond').style.left.replace('px',''))+190
               top 
    = parseInt($('cond').style.top.replace('px',''))+parseInt($('cond').style.height.replace('px',''))+28;
               width 
    = parseInt($('cond').style.width.replace('px',''));  
               border 
    = listBorder;  
               zIndex 
    = "1";   
               display
    ='none';
               backgroundColor 
    = unselectedBgColor;   
            }

            showDiv.onmouseover
    =function (){mouseLocation=1;};
            showDiv.onmouseout
    =function (){mouseLocation=0;};
            
    //overflow設置滾動條
            showDiv.innerHTML = "<div style='width:100%;height:150px;overflow:auto;'><table border='0' style='width: 100%;height:20%;font-size: 12;color:#33CC00;'><tbody id='showKeyList' style='margin-left: 0;margin-right: 0;margin-bottom: 0;margin-top: 0;'></tbody></table></div>";
            document.body.appendChild(showDiv);
            initKeyListState();
        }
      
        
        
    /*
            用途:設置相似關鍵字列表框是否可見
            參數:isDisplay,true表示可見,false表示不可見
        
    */
     
        
    function setDropListVisible(isDisplay){
            
    if (mouseLocation == 1){
                
    return;
            }

            
    if (($('cond').value.trim()!='')&&(isDisplay==true)){
                $('dropdownlistDiv').style.display
    ='';
            }

            
    else{
                $('dropdownlistDiv').style.display
    ='none';
            }

        }

        
        
    // 將創建相似關鍵字列表框方法附加到onload事件中
        if (window.addEventListener){
           window.addEventListener('load', createShowDiv, 
    false);
        }
    else if (window.attachEvent){
           window.attachEvent('onload', createShowDiv);
        }

        
    </script>
    這個js可以放在你需要實現搜索效果的jsp里,或單獨保存為js文件都可以.
    <div style="position:absolute;left:190px;top:25px;">
        
    <input AUTOCOMPLETE="off" 
            onkeydown
    ="oldKeyValue=this.value.trim();setSelectedKey();" 
            onkeyup
    ="getConformKey();" 
            onfocus
    ="if(this.value=='找人') this.value='';setDropListVisible(true);" 
            onblur
    ="setDropListVisible(false);"
            style
    ="width: 300; height: 23;z-index: 10;top:0;left:0;"  type="text" name="cond" value="找人" id="cond" />
        
    <input type="button" class="btn" value="搜一下"  onclick="findBylike();" />
    </div>
    useraction.findByLike(String name);是dao層的一個查詢方法,
    返回一個List,把這里換成你自己的實現就可以了.

    完美實現google搜索效果示例源碼下載

    posted on 2008-01-13 13:01 々上善若水々 閱讀(2466) 評論(3)  編輯  收藏 所屬分類: AJAX

    評論

    # re: DWR實現模擬Google搜索效果[未登錄]  回復  更多評論   

    為什么DWRUtil未定義的?
    2010-02-05 09:03 | ben

    # re: DWR實現模擬Google搜索效果  回復  更多評論   

    @ben
    報'DWRUtil'未定義,原因:在dwr.jar中,org.directwebremoting.ui.servlet包下的util.js文件中:if (typeof window['DWRUtil'] == 'undefined') window.DWRUtil = dwr.util;該句被注釋了
    解決方法一是取消對該句的注釋,二是在js函數加入該條語句。
    2010-03-19 14:21 | chenlei65368

    # re: DWR實現模擬Google搜索效果  回復  更多評論   

    @chenlei65368
    謝謝
    2010-03-29 16:33 | supercrsky
    主站蜘蛛池模板: 免费一级毛片无毒不卡| 成人久久久观看免费毛片| 精品一区二区三区免费毛片爱 | 大地资源二在线观看免费高清| 国产成人3p视频免费观看| 亚洲精品国产国语| 免费人成视频在线| 亚洲av无码专区首页| 永久免费无码网站在线观看 | 67pao强力打造67194在线午夜亚洲| 男女一边摸一边做爽的免费视频| 国产成人亚洲精品91专区手机| a高清免费毛片久久| 在线亚洲精品福利网址导航| a级毛片免费全部播放无码| 亚洲视频在线免费观看| 青青视频观看免费99| 亚洲av无码一区二区三区四区| 亚洲国产精品成人| 爱丫爱丫影院在线观看免费| 色婷婷亚洲十月十月色天| 免费观看AV片在线播放| 亚洲av无码专区亚洲av不卡| 久久精品国产亚洲Aⅴ香蕉| 久久久久免费看黄a级试看| 亚洲一区二区三区高清视频| 国产美女精品久久久久久久免费| 美女被爆羞羞网站在免费观看| 亚洲香蕉成人AV网站在线观看| 亚洲精品视频免费在线观看| 亚洲一区二区三区成人网站| 亚洲午夜无码AV毛片久久| 在线看无码的免费网站| 亚洲日本va一区二区三区| 亚洲中久无码不卡永久在线观看| 日韩午夜理论免费TV影院| 亚洲aⅴ天堂av天堂无码麻豆| 亚洲精品无码乱码成人| 日韩欧美一区二区三区免费观看 | 日韩免费观看一区| 一本色道久久88亚洲精品综合 |