防百度弹出层

103 篇文章 0 订阅
93 篇文章 0 订阅

// JScript 文件

 

var xmlHttp;
var currentLine = - 1;
var divtable;

function $(o)
{
   return document.getElementById(o);
}

String.prototype.trim = function()
{
   return this.replace(/(^[/s]*)|([/s]*$)/g, "");
}

function createXMLHttpRequest()
{
//开始初始化XMLHttpRequest 对象 
  if(window.XMLHttpRequest) { //Mozilla 浏览器 
     xmlHttp = new XMLHttpRequest(); 
     if (xmlHttp.overrideMimeType) {//设置MiME 类别         
         xmlHttp.overrideMimeType("text/xml"); 
     } 
  }else if (window.ActiveXObject) { // IE 浏览器 
     try { 
         xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (e) { 
          try { 
             xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
          } catch (e) {} 
     } 
  } 
  if (!xmlHttp) { // 异常,创建对象实例失败 
                   window.alert("不能创建XMLHttpRequest 对象实例."); 
                   return false; 
   } 
}

function getSearchResult(queryurl, values,event)
{
   if ( values.trim() != "")
   {
      var keyCodes = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;//兼容FF IE
    if(keyCodes != 38 && keyCodes != 40 && keyCodes != 32)
      {
         createXMLHttpRequest();
         url = queryurl + values.trim();
         xmlHttp.open("GET", url, true);
         xmlHttp.onreadystatechange = callback;
         xmlHttp.send(null);
      }
      if(e.keyCode == 32)
      {
        dropDiv();
      }
   }
   else
   {
        dropDiv();
   }
}

function dropDiv()
{
   if($("contents"))
   {
      document.body.removeChild(divtable);
   }
}

function callback()
{
   if(xmlHttp.readyState == 4)
   {
      if(xmlHttp.status == 200)
      {
         if(xmlHttp.responseText.trim() == "")
         {
            dropDiv();
         }
         else
         {
            createTable(xmlHttp.responseText);
         }
      }
   }
}


function createTable(strtable)
{

   if($("contents"))
   {
   document.body.removeChild(divtable);
   }
   divtable = document.createElement("div");
   divtable.setAttribute("id", "contents");
   divtable.style.position = "absolute";
   divtable.style.border = "1px solid #817F82";
   divtable.style.background = "#fff";
   divtable.style.fontSize = "12px";
   divtable.innerHTML = strtable;
   divtable.zIndex = "400";
   divtable.style.width = $("txtkecode").offsetWidth + 80 + "px";
   divtable.style.overflowY = "auto";
   divtable.style.overflowX = "hidden";
   divtable.style.height = "200px";
   divtable.style.top = $("txtkecode").offsetTop + $("txtkecode").offsetHeight + "px";
   divtable.style.left = $("txtkecode").offsetLeft - 4 + "px";
   document.body.appendChild(divtable);
}


document.onkeydown = function(e)
{
   if(document.getElementById("ice"))
   {
      e = window.event || e;
      switch(e.keyCode)
      {
         case 38 :
            currentLine -- ;
            changeItem();
            break;
         case 40 :
            currentLine ++ ;
            changeItem();
            break;
         case 32 :
            document.body.appendChild(divtable);
            break;
         default :
            break;
      }
   }
}

document.onclick = function()
{
    dropDiv();
}

function changeItem()
{
   if(document.all)
   {
      var it = document.getElementById("ice").children[0];
   }
   else
   {
      var it = document.getElementById("ice");
   }
   for(var j = 0; j < it.rows.length; j ++ )
   {
      it.rows[j].className = "";
   }
   if(currentLine < 0)
   {
      currentLine = it.rows.length - 1;
   }
   if(currentLine == it.rows.length)
   {
      currentLine = 0;
   }
   it.rows[currentLine].className = "highlight";
   $("txtkecode").value = it.rows[currentLine].cells[0].innerHTML.split('-')[0];
   $("txtkename").value = it.rows[currentLine].cells[0].innerHTML.split('-')[1];
}


function clickItem(clicktLine)
{
   if(document.all)
   {
      var it = document.getElementById("ice").children[0];
   }
   else
   {
      var it = document.getElementById("ice");
   }
   for(i = 0; i < it.rows.length; i ++ )
   {
      it.rows[i].className = "";
   }
   it.rows[clicktLine].className = "highlight";

   $("txtkecode").value = it.rows[clicktLine].cells[0].innerHTML.split('-')[0];
   $("txtkename").value = it.rows[clicktLine].cells[0].innerHTML.split('-')[1];

   it.rows[clicktLine].cells[0].onclick = function()
   {
      $("txtkecode").value = it.rows[clicktLine].cells[0].innerHTML.split('-')[0];
      $("txtkename").value = it.rows[clicktLine].cells[0].innerHTML.split('-')[1];
      document.body.removeChild(divtable);
   }
}

 

 

页面使用

 

 <input type="text" id="txtkecode" name="txtkecode" οnkeyup="javascript:getSearchResult('do/Lession.aspx?daima=',this.value,event);"
                            style="position: relative;" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值