最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JSuggest自動匹配下拉框使用方法(示例代碼)

 更新時(shí)間:2013年12月27日 08:56:30   作者:  
本篇文章主要是對JSuggest自動匹配下拉框使用方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助

1.下載jquery-latest.js,JSuggest.js和JSuggest.css

JSuggest.js源代碼如下

復(fù)制代碼 代碼如下:

/**
*
* Description : JSuggest 下拉提示框
*/

function JSuggest(){

// DIV下拉框
this.div = null;

// DIV下的ul
this.ul = null;

// 文本輸入框
this.input = null;

// 當(dāng)前DIV所選的LI對象
this.current_li = null;

/**
* 隱藏下拉提示框
*/
this.hide = function(){
this.div.style.visibility = "hidden";
}

/**
* 顯示下拉提示框
*/
this.show = function(){
this.div.style.visibility = "visible";
}

/**
* 下拉提示框狀態(tài)
*/
this.status = function(){
if (this.div.style.visibility == "visible"){
return true;
}
return false;
}

/**
* 設(shè)置當(dāng)前DIV所選的LI
*/
this.setCurrent_li = function(li, obj){
var co = obj.current_li;
if (co != null){
co.className = "";
}
li.className = "li_index";
obj.current_li = li;
}

/**
* 初始化Suggest
*
* input_id : 輸入框的ID
* defHeight: 下拉提示框的高(不提供也可以)
*/
this.init = function(input_id, defHeight){
this.input = document.getElementById(input_id);
//this.input.autocomplete = "off";
var left = this.input.offsetLeft;
var top = this.input.offsetTop;
var width = this.input.offsetWidth;
var height = this.input.offsetHeight;
var p=this.input.offsetParent;
   while(p!= null){
left+=p.offsetLeft;
top+=p.offsetTop;
p=p.offsetParent;
   }
top+= height;
if(defHeight==null || defHeight==0){
height = 150;
}else{
height = defHeight;
}
this.input.value = "";
var obj = this;
this.input.onkeydown = function(evt){
obj.onkeydown(evt, obj);
}
this.div = document.createElement("div");
this.div.style.width = width + "px";
this.div.style.height = height + "px";
this.div.style.left = left + "px";
this.div.style.top = top + "px";
this.ul = document.createElement("ul");
this.div.appendChild(this.ul);
this.div.className = "jsuggest";
document.body.appendChild(this.div);
}

/**
* 移除DIV下UL中所有的LI
*/
this.remove = function(){
this.current_li = null;
while(this.removeLI());
}

/**
* 移除DIV下UL中的LI
*/
this.removeLI = function(){
var node = this.ul.childNodes;
for(var n in node){
if (node[n] != null && node[n].nodeName == "LI"){
// alert(node[n].innerHTML);
this.ul.removeChild(node[n]);
return true;
}
}
return false;
}

/**
* 在DIV中創(chuàng)建LI
*/
this.create = function(items){
this.remove();

var li_item = items.split(",");

for(var i in li_item){
//alert(li_item[i]);
var li = document.createElement("li");
li.innerHTML = li_item[i];

var obj = this;
li.onmousedown = function(){
obj.onmousedown(this, obj);
}
li.onmouseover = this.onmouseover;
li.onmouseout = this.onmouseout;

this.ul.appendChild(li);
}
this.show();
}

/**
* 文本框按下事件
*/
this.onkeydown = function(evt, obj){
if (!obj.status()){
return false;
}

if (!evt && window.event)
{
evt = window.event;
}

var key = evt.keyCode;

//var KEYUP = 38;
//var KEYDOWN = 40;
//var KEYENTER = 13;
var ob = obj;
if (key == 38){
obj.upKeySelected();
}else if (key == 40){
obj.downKeySelected();
}else if (key == 13 || key == 27){
obj.hide();
}
}

this.getCurrentLiIndex = function(){
if(this.current_li == null){
return -1;
}
var node = this.ul.childNodes;
for(var n in node){
if (node[n].nodeName == "LI"){
if(node[n] == this.current_li){
return n;
}
}
}
}

this.getLi = function(index){
var node = this.ul.childNodes;
for(var n in node){
if (node[n].nodeName == "LI" && n == index){
this.setCurrent_li(node[n], this);
return node[n];
}
}
}

this.upKeySelected = function(){
var num = this.getCurrentLiIndex();
if (num != -1 && num > 0){
num--;
var node = this.getLi(num);
this.setCurrent_li(node, this);
this.input.value = node.innerHTML;
}
}

this.downKeySelected = function(obj){
var num = this.getCurrentLiIndex();
if (num == -1){
num = 0;
}else {
num++;
if (num >= this.ul.childNodes.length)return false;
}
var node = this.getLi(num);
this.setCurrent_li(node, this);
this.input.value = node.innerHTML;
}

/**
* DIV鼠標(biāo)按下事件
*/
this.onmousedown = function(thiz, obj){
obj.setCurrent_li(thiz, obj);
obj.input.value = thiz.innerHTML;
obj.hide();
}

/**
* DIV鼠標(biāo)移動事件
*/
this.onmouseover = function(){
if (this.className != "li_index"){
this.className = "li_check";
}
}

/**
* DIV鼠標(biāo)移出事件
*/
this.onmouseout = function(){
if (this.className == "li_check"){
this.className = "";
}
}

}

var jsuggest = new JSuggest();


2.jsp頁面
復(fù)制代碼 代碼如下:

<input id="text" name="text" type="text"  onkeyup="go(event, this.value);"/>

<script type="text/javascript">   

 j(document).ready(function(){
  // 初始化JSUGGEST
  jsuggest.init("text");
  //或者用下面的方法,設(shè)置下拉框高度
  //jsuggest.init("text",200);
 })

 
 function go(event, value){
     event= event ? event : (window.event ? window.event : arguments[0]);
     var url = "url?suggestInput="+encodeURIComponent(value);//url是具體的action或jsp地址等,返回值是以逗號分隔的字符串
  goSuggestInput(event,url,value);
 }

 

function goSuggestInput(evnet,url,value){
        if (value == ""){
         // 如果輸入框?yàn)榭针[藏下拉框
         jsuggest.hide();
      return false;   
        }

     // 確保evt是一個(gè)有效的事件  
  if (!evnet && window.event)
  {
   evnet = window.event;
  }
  var key = evnet.keyCode;
  if (key == 38 || key == 40 || key == 13 || key == 27){
   return false;
  }

  j.ajax({
   type: "post",
         url:url,
   dataType: "text",
   cache: "false",

   beforeSend: function(XMLHttpRequest){
   },
   success: function(data, textStatus){
    // 對下拉框添加數(shù)據(jù)
    jsuggest.create(data);

   },
   complete: function(XMLHttpRequest, textStatus){

   },
   error: function(){
    alert("對不起,服務(wù)器忙!");
   }
  });
 }

</script>

相關(guān)文章

  • 在window.setTimeout方法中傳送對象

    在window.setTimeout方法中傳送對象

    setTimeout方法是js中的延時(shí)方法,很多js的bug,只需要使用該方法延時(shí)一下,就會自動解決了,簡直就是萬能藥方,也是我比較喜歡使用的最后手段。
    2006-12-12
  • js實(shí)現(xiàn)數(shù)組轉(zhuǎn)換成json

    js實(shí)現(xiàn)數(shù)組轉(zhuǎn)換成json

    本文給大家分享的是使用javascript實(shí)現(xiàn)的數(shù)組轉(zhuǎn)換json的代碼,非常簡單實(shí)用,相當(dāng)于JSON.stringify(array);,有需要的小伙伴可以參考下。
    2015-06-06
  • javascript實(shí)現(xiàn)雙端隊(duì)列

    javascript實(shí)現(xiàn)雙端隊(duì)列

    這篇文章主要為大家詳細(xì)介紹了使用javascript實(shí)現(xiàn)雙端隊(duì)列,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • javascript面向?qū)ο蟀b類Class封裝類庫剖析

    javascript面向?qū)ο蟀b類Class封裝類庫剖析

    一個(gè)從來沒有接觸過javascript的技術(shù)人員,幾小時(shí)內(nèi)就可以寫出一個(gè)簡單有用的程序代碼;想寫出高性能的代碼,同樣需要具備一個(gè)高級程序員的基本素養(yǎng),javascript也是如此
    2013-01-01
  • 模仿百度三維地圖的js數(shù)據(jù)分享

    模仿百度三維地圖的js數(shù)據(jù)分享

    模仿百度三維地圖的js數(shù)據(jù),需要的朋友可以參考下。
    2011-05-05
  • js打印紙函數(shù)代碼(遞歸)

    js打印紙函數(shù)代碼(遞歸)

    js打印紙函數(shù)代碼,獲取并設(shè)置打印紙的具體信息的代碼,需要的朋友可以參考下。
    2010-06-06
  • APP中javascript+css3實(shí)現(xiàn)下拉刷新效果

    APP中javascript+css3實(shí)現(xiàn)下拉刷新效果

    本文給大家分享的是如何在APP中使用javascript結(jié)合CSS3實(shí)現(xiàn)下拉刷新特效的代碼,非常的簡單實(shí)用,有需要的小伙伴可以參考下。
    2016-01-01
  • 微信小程序?qū)崿F(xiàn)彈球游戲

    微信小程序?qū)崿F(xiàn)彈球游戲

    這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)彈球游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • js實(shí)現(xiàn)可拖動DIV的方法

    js實(shí)現(xiàn)可拖動DIV的方法

    這篇文章主要介紹了js實(shí)現(xiàn)可拖動DIV的方法,有需要的朋友可以參考一下
    2013-12-12
  • JS生成唯一id方式之UUID和NanoID

    JS生成唯一id方式之UUID和NanoID

    這篇文章主要介紹了JS生成唯一id方式之UUID和NanoID,唯一id有了更好的方式,那就是NanoID,今天這篇文章記錄一下NanoID 正在取代 UUID?及js 生成uuid及NanoID的方法,下面來看文章的詳細(xì)介紹
    2021-12-12

最新評論

吕梁市| 娱乐| 广水市| 汕尾市| 古丈县| 瓦房店市| 松原市| 河北区| 平邑县| 孟村| 钟祥市| 尼勒克县| 筠连县| 喀什市| 周宁县| 武鸣县| 通河县| 桂东县| 南木林县| 济南市| 贵州省| 陕西省| 寿阳县| 常州市| 罗定市| 台前县| 冷水江市| 平利县| 都匀市| 宽城| 旬邑县| 辽宁省| 尚志市| 馆陶县| 临泽县| 陵川县| 正宁县| 石渠县| 石狮市| 铁岭市| 九台市|