通過鍵盤上下鍵 JS事件,控制候選詞的選擇項


效果圖

JS代碼

    //上下鍵 選擇事件  searchBackgroud 為樣式,只做標記,無實質樣式,因為和其他樣式不兼容,只能添加CSS
    $(document).keydown(function (event) {
        var upDownClickNum = $("#SearchTips .searchBackgroud ").length;
        if ($("#SearchTips").css("display") == "block") {
            alert(event.keyCode)
            //38:上  40:下
            if (event.keyCode == 38) {
                if (upDownClickNum < 1) {
                    $("#SearchTips li:last a").css({ "background": "#f0f0f0" }).addClass("searchBackgroud");
                } else {
                    $("#SearchTips .searchBackgroud ").removeClass("searchBackgroud").css({ "background": "" }).parent().prev().children().addClass("searchBackgroud").css({ "background": "#f0f0f0" });
                }

                stopDefault(event);//不阻止光標戶向前移動
            } else if (event.keyCode == 40) {
                if (upDownClickNum < 1) {
                    $("#SearchTips li:first a").css({ "background": "#f0f0f0" }).addClass("searchBackgroud");
                } else {
                    $("#SearchTips .searchBackgroud").removeClass("searchBackgroud").css({ "background": "" }).parent().next().children().addClass("searchBackgroud").css({ "background": "#f0f0f0" });

                }
                stopDefault(event);
            }
            upDownClickNum++;

        }


    });

    //阻止事件執行
    function stopDefault(e) {

        //阻止默認瀏覽器動作(W3C)   
        if (e && e.preventDefault) {
            //火狐的 事件是傳進來的e  
            e.preventDefault();
        }
            //IE中阻止函數器默認動作的方式   
        else {
            //ie 用的是默認的event  
            event.returnValue = false;
        }
    }

 

html代碼

<html>
 <head></head>
 <body>
  <ol id="SearchTips" class="search_list" style="display: none;"> 
   <li> <a href="/VIEW/HouseSecond/HouseSecondList.html?r=1&amp;c=138&amp;rs=鳳凰北新村1區" onclick="ga('send', 'pageview', '1');">鼓樓 鳳凰北新村<span><b>1</b> </span>區 西江濱 待售(1) </a> </li> 
   <li> <a href="/VIEW/HouseSecond/HouseSecondList.html?r=1&amp;c=122&amp;rs=鳳凰北新村1區" onclick="ga('send', 'pageview', '1');">鼓樓 鳳凰北新村<span><b>1</b> </span>區 萬寶商圈 待售(1) </a> </li> 
   <li> <a href="/VIEW/HouseSecond/HouseSecondList.html?r=5&amp;c=129&amp;rs=怡美嘉園1期" onclick="ga('send', 'pageview', '1');">倉山 怡美嘉園<span><b>1</b> </span>期[怡美嘉園] 倉山步行街 待售(3)</a></li> 
   <li> <a href="/VIEW/HouseSecond/HouseSecondList.html?r=5&amp;c=146&amp;rs=怡美嘉園1期" onclick="ga('send', 'pageview', '1');">倉山 怡美嘉園<span><b>1</b> </span>期[怡美嘉園] 三叉街 待售(3) </a></li> 
   <li> <a href="/VIEW/HouseSecond/HouseSecondList.html?r=3&amp;c=127&amp;rs=福興花園1期" onclick="ga('send', 'pageview', '1');">晉安 福興花園<span><b>1</b> </span>期 鼓山新東區 待售(7)</a> </li> 
   <li> <a href="/VIEW/HouseSecond/HouseSecondList.html?r=3&amp;c=141&amp;rs=福興花園1期" onclick="ga('send', 'pageview', '1');">晉安 福興花園<span><b>1</b> </span>期 福馬路 待售(7)</a></li>
   <li> <a href="/VIEW/HouseSecond/HouseSecondList.html?r=7&amp;rs=融匯山水1期" onclick="ga('send', 'pageview', '1');">閩侯 融匯山水<span><b>1</b> </span>期 待售(32)</a></li> 
   <li> <a href="/VIEW/HouseSecond/HouseSecondList.html?r=5&amp;rs=振興花園1期">倉山 振興花園<span><b>1</b> </span></a></li>
   <li> <a href="/VIEW/HouseSecond/HouseSecondList.html?r=3&amp;c=155&amp;rs=彩虹鋪景" onclick="ga('send', 'pageview', '1');">晉安 彩虹鋪景[隆盛小區 <span><b>1</b> </span>#樓] 五四路(溫泉公園) 待售(6)</a></li>
   <li> <a href="/VIEW/HouseSecond/HouseSecondList.html?r=3&amp;c=142&amp;rs=彩虹鋪景" onclick="ga('send', 'pageview', '1');">晉安 彩虹鋪景[隆盛小區 <span><b>1</b> </span>#樓] 火車站 待售(6) </a></li>
  </ol>
 </body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM