动态生成文本提示下拉框

  1. 基本思路是:
  2. 通过文本框的位置来确定提示框的位置
  3. 提示框用div来实现
  4. 文本框第一次获得获得焦点时用ajax读取数据用数组保存
  5. 通过对文本框添加onkeyup事件来刷新提示框的数据

  6. /创建XMLHttpRequest对象///
  7. var xmlHttp = false;
  8. var nameVal = new Array();
  9. if(window.ActiveXObject){
  10.       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  11.   }
  12. else if(window.XMLHttpRequest){
  13.       xmlHttp = new XMLHttpRequest();
  14.   }
  15. else
  16.       xmlHttp = false;
  17. }     
  18. //为id为staion_key输入框绑定事件.
  19. function addIndexStation(){
  20. if(document.getElementById("station_key")) return;
  21. var station = document.getElementById("station_key");
  22.     station.onkeyup = function(){       
  23.         showInfoBox(this,nameVal);
  24.         if(nameVal.length == 0) getDateForS();
  25.     }
  26. }
  27. ///自动提示框事件函数///
  28. function showInfoBox(objct,ary){    //objct为输入框,ary为数据数组
  29.     createInfoBox(objct);
  30.     var divInfoBox = document.getElementById("divinfobox"); 
  31.     divInfoBox.style.display = "none";
  32.     var oldvalue = objct.value;
  33.     var newvalue = "";   
  34.     
  35.     if(divInfoBox.style.display == "none"
  36.     if(oldvalue==""return;
  37.     for(var i=0;i<ary.length;i++){
  38.         if(ary[i].indexOf(oldvalue)>=0)
  39.         newvalue += ('<li>'+ary[i]+'</li>');
  40.         
  41.     }
  42.     if(newvalue == ""return;  
  43.     divInfoBox.innerHTML = "<ul>"+newvalue+"</ul>";
  44.     divInfoBox.style.display = "block"
  45.     
  46. /添加鼠标移到提示框的事件处理/
  47.         if(!document.getElementById("divinfobox"))return;
  48.         var divInfoBox = document.getElementById("divinfobox");
  49.         var ul = divInfoBox.firstChild;    
  50.         var li_all = ul.childNodes;    
  51.         for(var j=0;j<li_all.length;j++){
  52.                 li_all[j].onmousemove = function(){
  53.                               objct.value = this.firstChild.nodeValue;
  54.                                 }
  55.         }   
  56. }
  57. function getDateForS(){ 
  58.      var queryString="key="'';               
  59.      xmlHttp.open("POST",baseUrl+"start/getStations",true);
  60.      xmlHttp.onreadystatechange = setStations;
  61.     //make the server request
  62.     xmlHttp.setRequestHeader('Content-Type''application/x-www-form-urlencoded');
  63.     xmlHttp.send(queryString);    
  64.    //getdatetime=setTimeout("getDateForS(oldvalue)", 500);
  65. }
  66. function setStations(){
  67.     if (xmlHttp.readyState == 4) 
  68.   {
  69.     // status of 200 indicates the transaction completed successfully
  70.     if (xmlHttp.status == 200) 
  71.     {
  72.      var str= xmlHttp.responseText; 
  73.      nameVal = str.split(',');    
  74.     } 
  75.     // a HTTP status different than 200 signals an error
  76.     else 
  77.     {
  78.       alert("There was a problem accessing the server: " + xmlHttp.statusText);
  79.     }
  80.   }
  81. }
  82. 创建提示框
  83. function createInfoBox(inputTxt){
  84.             if(document.getElementById("divinfobox")) return;
  85.             var pos = getElementPos(inputTxt);  
  86.             var divInfoBox=document.createElement("div");           
  87.             var width = inputTxt.style.width;
  88.             divInfoBox.style.width = width;
  89.             divInfoBox.setAttribute("id","divinfobox");         
  90.             addClass(divInfoBox,"infobox_1");           
  91.             //divInfoBox.style.height = "100px";            
  92.             divInfoBox.style.position = "absolute";
  93.             divInfoBox.style.top = pos.y + 18 + "px";
  94.             divInfoBox.style.left = pos.x + "px";               
  95.             divInfoBox.style.backgroundColor = "#ffffff";       
  96.             if(!inputTxt.parentNode){ alert('没有父节点'); return;}      
  97.             var inputTxtParNod=inputTxt.parentNode;         
  98.             inputTxtParNod.insertBefore(divInfoBox,inputTxt); 
  99. }
  100. //获得元素对象位置//
  101. function getElementPos(el) {
  102.       var ua = navigator.userAgent.toLowerCase();
  103.       var isOpera = (ua.indexOf('opera') != -1);
  104.       var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof
  105.   
  106.      if(el.parentNode === null || el.style.display == 'none')
  107.        {
  108.           return false;
  109.       }
  110.   
  111.       var parent = null;
  112.       var pos = [];
  113.       var box;
  114.       if(el.getBoundingClientRect) //IE
  115.        {
  116.           box = el.getBoundingClientRect();
  117.           var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
  118.           var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
  119.            return {x:box.left + scrollLeft, y:box.top + scrollTop};
  120.       }
  121.       else if(document.getBoxObjectFor) // gecko
  122.        {
  123.           box = document.getBoxObjectFor(el);
  124.           var borderLeft = (el.style.borderLeftWidth)?parseInt(el.style.borderLeftWidth):0;
  125.           var borderTop = (el.style.borderTopWidth)?parseInt(el.style.borderTopWidth):0;
  126.           pos = [box.x - borderLeft, box.y - borderTop];
  127.       }
  128.       else // safari & opera
  129.        {
  130.           pos = [el.offsetLeft, el.offsetTop];
  131.           parent = el.offsetParent;
  132.           if (parent != el) 
  133.            {
  134.   
  135.            while (parent) {
  136.               pos[0] += parent.offsetLeft;
  137.               pos[1] += parent.offsetTop;
  138.               parent = parent.offsetParent;
  139.           }
  140.       }
  141.       if (ua.indexOf('opera') != -1
  142.       || ( ua.indexOf('safari') != -1 && el.style.position == 'absolute' ))
  143.        {
  144.           pos[0] -= document.body.offsetLeft;
  145.           pos[1] -= document.body.offsetTop;
  146.       }
  147.       }
  148.   
  149.        if (el.parentNode) { parent = el.parentNode; }
  150.        else { parent = null; }
  151.       while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML')
  152.        { // account for any scrolled ancestors
  153.       pos[0] -= parent.scrollLeft;
  154.       pos[1] -= parent.scrollTop;
  155.        if (parent.parentNode) { parent = parent.parentNode; }
  156.        else { parent = null; }
  157.       }
  158.        return {x:pos[0], y:pos[1]};
  159.   }
  160. /加类样式函数/
  161.  function addClass(element,value) { 
  162.   if (!element.className) { 
  163.     element.className = value; 
  164.   } else { 
  165.     newClassName = element.className; 
  166.     newClassName+= " "
  167.     newClassName+= value; 
  168.     element.className = newClassName; 
  169.   } 
  170. /加载函数///
  171. function addLoadEvent(func) 
  172. {
  173.   var oldonload = window.onload;
  174.   if (typeof window.onload != 'function'
  175.   {
  176.     window.onload = func;
  177.   } 
  178.   else 
  179.   {
  180.     window.onload = function() 
  181.     {
  182.       oldonload();
  183.       func();
  184.     }
  185.   } 
  186. }
  187. addLoadEvent(addIndexStation);
样式表:
  1. .infobox_1{
  2.     border:#999999 1px solid;
  3.     width:150px;
  4.     height:auto;        
  5.     }
  6. .infobox_1 li{  
  7.     width:100%;
  8.     text-align:left;
  9.     overflow:hidden;
  10.     color:#000000;
  11.     }
  12. .infobox_1 li:hover {
  13.     color:#000000;      
  14.     background:#00CCFF
  15.     }
  16.     
初学js一定有很多不足之处,请多多指教!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值