- 基本思路是:
- 通过文本框的位置来确定提示框的位置
- 提示框用div来实现
- 文本框第一次获得获得焦点时用ajax读取数据用数组保存
- 通过对文本框添加onkeyup事件来刷新提示框的数据
- /创建XMLHttpRequest对象///
- var xmlHttp = false;
- var nameVal = new Array();
- if(window.ActiveXObject){
- xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
- }
- else if(window.XMLHttpRequest){
- xmlHttp = new XMLHttpRequest();
- }
- else{
- xmlHttp = false;
- }
- //为id为staion_key输入框绑定事件.
- function addIndexStation(){
- if(document.getElementById("station_key")) return;
- var station = document.getElementById("station_key");
- station.onkeyup = function(){
- showInfoBox(this,nameVal);
- if(nameVal.length == 0) getDateForS();
- }
- }
- ///自动提示框事件函数///
- function showInfoBox(objct,ary){ //objct为输入框,ary为数据数组
- createInfoBox(objct);
- var divInfoBox = document.getElementById("divinfobox");
- divInfoBox.style.display = "none";
- var oldvalue = objct.value;
- var newvalue = "";
- if(divInfoBox.style.display == "none")
- if(oldvalue=="") return;
- for(var i=0;i<ary.length;i++){
- if(ary[i].indexOf(oldvalue)>=0)
- newvalue += ('<li>'+ary[i]+'</li>');
- }
- if(newvalue == "") return;
- divInfoBox.innerHTML = "<ul>"+newvalue+"</ul>";
- divInfoBox.style.display = "block";
- /添加鼠标移到提示框的事件处理/
- if(!document.getElementById("divinfobox"))return;
- var divInfoBox = document.getElementById("divinfobox");
- var ul = divInfoBox.firstChild;
- var li_all = ul.childNodes;
- for(var j=0;j<li_all.length;j++){
- li_all[j].onmousemove = function(){
- objct.value = this.firstChild.nodeValue;
- }
- }
- }
- function getDateForS(){
- var queryString="key="+ '';
- xmlHttp.open("POST",baseUrl+"start/getStations",true);
- xmlHttp.onreadystatechange = setStations;
- //make the server request
- xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
- xmlHttp.send(queryString);
- //getdatetime=setTimeout("getDateForS(oldvalue)", 500);
- }
- function setStations(){
- if (xmlHttp.readyState == 4)
- {
- // status of 200 indicates the transaction completed successfully
- if (xmlHttp.status == 200)
- {
- var str= xmlHttp.responseText;
- nameVal = str.split(',');
- }
- // a HTTP status different than 200 signals an error
- else
- {
- alert("There was a problem accessing the server: " + xmlHttp.statusText);
- }
- }
- }
- 创建提示框
- function createInfoBox(inputTxt){
- if(document.getElementById("divinfobox")) return;
- var pos = getElementPos(inputTxt);
- var divInfoBox=document.createElement("div");
- var width = inputTxt.style.width;
- divInfoBox.style.width = width;
- divInfoBox.setAttribute("id","divinfobox");
- addClass(divInfoBox,"infobox_1");
- //divInfoBox.style.height = "100px";
- divInfoBox.style.position = "absolute";
- divInfoBox.style.top = pos.y + 18 + "px";
- divInfoBox.style.left = pos.x + "px";
- divInfoBox.style.backgroundColor = "#ffffff";
- if(!inputTxt.parentNode){ alert('没有父节点'); return;}
- var inputTxtParNod=inputTxt.parentNode;
- inputTxtParNod.insertBefore(divInfoBox,inputTxt);
- }
- //获得元素对象位置//
- function getElementPos(el) {
- var ua = navigator.userAgent.toLowerCase();
- var isOpera = (ua.indexOf('opera') != -1);
- var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof
- if(el.parentNode === null || el.style.display == 'none')
- {
- return false;
- }
- var parent = null;
- var pos = [];
- var box;
- if(el.getBoundingClientRect) //IE
- {
- box = el.getBoundingClientRect();
- var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
- var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
- return {x:box.left + scrollLeft, y:box.top + scrollTop};
- }
- else if(document.getBoxObjectFor) // gecko
- {
- box = document.getBoxObjectFor(el);
- var borderLeft = (el.style.borderLeftWidth)?parseInt(el.style.borderLeftWidth):0;
- var borderTop = (el.style.borderTopWidth)?parseInt(el.style.borderTopWidth):0;
- pos = [box.x - borderLeft, box.y - borderTop];
- }
- else // safari & opera
- {
- pos = [el.offsetLeft, el.offsetTop];
- parent = el.offsetParent;
- if (parent != el)
- {
- while (parent) {
- pos[0] += parent.offsetLeft;
- pos[1] += parent.offsetTop;
- parent = parent.offsetParent;
- }
- }
- if (ua.indexOf('opera') != -1
- || ( ua.indexOf('safari') != -1 && el.style.position == 'absolute' ))
- {
- pos[0] -= document.body.offsetLeft;
- pos[1] -= document.body.offsetTop;
- }
- }
- if (el.parentNode) { parent = el.parentNode; }
- else { parent = null; }
- while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML')
- { // account for any scrolled ancestors
- pos[0] -= parent.scrollLeft;
- pos[1] -= parent.scrollTop;
- if (parent.parentNode) { parent = parent.parentNode; }
- else { parent = null; }
- }
- return {x:pos[0], y:pos[1]};
- }
- /加类样式函数/
- function addClass(element,value) {
- if (!element.className) {
- element.className = value;
- } else {
- newClassName = element.className;
- newClassName+= " ";
- newClassName+= value;
- element.className = newClassName;
- }
- }
- /加载函数///
- function addLoadEvent(func)
- {
- var oldonload = window.onload;
- if (typeof window.onload != 'function')
- {
- window.onload = func;
- }
- else
- {
- window.onload = function()
- {
- oldonload();
- func();
- }
- }
- }
- addLoadEvent(addIndexStation);
- .infobox_1{
- border:#999999 1px solid;
- width:150px;
- height:auto;
- }
- .infobox_1 li{
- width:100%;
- text-align:left;
- overflow:hidden;
- color:#000000;
- }
- .infobox_1 li:hover {
- color:#000000;
- background:#00CCFF;
- }