javascript仿百度輸入框提示自動下拉補全,javascript補全

來源:互聯網
上載者:User

javascript仿百度輸入框提示自動下拉補全,javascript補全

本文執行個體講解了javascript輸入框自動下拉補全操作,仿百度、Google搜尋方塊提示,具體內容如下

:

具體代碼:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> <script type="text/javascript" src="jquery.js"></script>  <script type="text/javascript">  /*自動下拉補全 zhk */  var highlightindex=-1;//當前高亮的節點 $(document).ready(function(){ var wordInput=$("#word"); var wordInputOffset=wordInput.offset();   $("#auto").hide().css("border","1px black solid").css("position","absolute")  .css("top",wordInputOffset.top+wordInput.height()+5+"px")  .css("left",wordInputOffset.left+"px").width(wordInput.width()+2);  wordInput.keyup(function (event){   var myEvent=event||window.event;  var keyCode=myEvent.keyCode;  if(keyCode>=65&&keyCode<=90||keyCode==8||keyCode==46){    var wordText=$("#word").val();   var autoNode=$("#auto");   if(wordText!=""){           var wordNodes=$("span");        autoNode.html("");    wordNodes.each(function(i){     var wordNode=$(this);     var newDivNode=$("<div>").attr("id",i);           newDivNode.html(wordNode.text()).appendTo(autoNode);     newDivNode.mouseover(function(){//滑鼠進入      if(highlightindex!=-1){        $("#auto").children("div").eq(highlightindex)        .css("background-color","white");       }       highlightindex=$(this).attr("id");       $(this).css("background-color","red");      })       newDivNode.mouseout(function(){//滑鼠移除       $(this).css("background-color","white");             })newDivNode.click(function(){//點擊       var comText=$(this).text();       $("#auto").hide();     highlightindex=-1;     $("#word").val(comText);       })     })    if(wordNodes.length>0){     autoNode.show();    }else{     autoNode.hide();     highlightindex=-1;     }      }else{   autoNode.hide();    highlightindex=-1;    }    }else if(keyCode==38||keyCode==40){     if(keyCode==38){//向上      var autoNodes=$("#auto").children("div");       if(highlightindex!=-1){        autoNodes.eq(highlightindex).css("background-color","white");         highlightindex--;       }else{        highlightindex=autoNodes.length-1;       }              if(highlightindex==-1){        highlightindex=autoNodes.length-1;       }       autoNodes.eq(highlightindex).css("background-color","red");     }     if(keyCode==40){      var autoNodes=$("#auto").children("div");       if(highlightindex!=-1){        autoNodes.eq(highlightindex).css("background-color","white");       }       highlightindex++;       if(highlightindex==autoNodes.length){        highlightindex=0;       }       autoNodes.eq(highlightindex).css("background-color","red");     }   }else if(keyCode==13){         if(highlightindex!=-1){     var comText=$("#auto").hide().children("div").eq(highlightindex).text();     highlightindex=-1;     $("#word").val(comText);     }else{      alert("文字框中的【"+$("#word").val()+"】被提交了");      $("#auto").hide();       $("#word").get(0).blur();//失去焦點     }    }  });   $("input [type='button']").click(function(){   alert("文字框中的【"+$("#word").val()+"】被提交了");  });  })</script> </HEAD>  <BODY> <input type="text" id="word"><input type="button" value="提交"><div id="auto"></div><p><span>aaa</span><span>abc</span><span>abd</span><span>bbc</span><span>beb</span><span>cer</span><span>erd</span><span>beg</span><p> </BODY></HTML>

以上就是本文的全部內容,希望對大家學習javascript程式設計有所協助。

您可能感興趣的文章:
  • javascript 輸入框提示列表效果
  • JSP + ajax實現輸入框自動補全功能 執行個體代碼
  • 仿百度輸入框智能提示的js代碼
  • js輸入框郵箱自動提示功能代碼實現
  • js操作輸入框提示資訊且響應滑鼠事件
  • JS實現仿google、百度搜尋方塊輸入資訊智能提示的實現方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.