jQuery之自動完成組件的深入解析
更新時間:2013年06月19日 16:13:07 作者:
本篇文章是對jQuery中的自動完成組件進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
簡單實例
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>AutocompleteOption</title>
<link rel="stylesheet" type="text/css" href="themes/base/jquery.ui.all.css"/>
<script type="text/javascript" src="JS/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="JS/jquery.ui.core.js"></script>
<script type="text/javascript" src="JS/jquery.ui.widget.js"></script>
<script type="text/javascript" src="JS/jquery.ui.position.js"></script>
<script type="text/javascript" src="JS/jquery.ui.autocomplete.js"></script>
<script type="text/javascript">
$(document).ready(function(){
/* 初始化數(shù)據(jù)源 */
var keys = ["jsp", "javascript", "jquery", "asp", "asp.net", "php",];
$('#searchBox').autocomplete({
source : keys,
minLength : 2
});
});
</script>
<style>
body{ padding:30px; }
</style>
</head>
<body>
<input id="searchBox" />
</body>
</html>
效果圖:

在上述代碼中,在頁面初始化的時候?qū)㈨撁嫔系妮斎肟虬b成jQuery對象,然后使用autocomplete()方法將其包裝成自動完成組件,同時初始化其最小響應(yīng)長度選項和數(shù)據(jù)源選項
2:自動完成組件的方法
有Search, Open, Focus, Select, Close, Change事件
復(fù)制代碼 代碼如下:
function(event, ui) {
//event: 觸發(fā)事件時的事件對象
//ui, 是用戶界面對象,ui.item是一個包含label和value屬性的對象
}
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>AutocompleteEvent</title>
<link rel="stylesheet" type="text/css" href="themes/base/jquery.ui.all.css"/>
<script type="text/javascript" src="JS/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="JS/jquery.ui.core.js"></script>
<script type="text/javascript" src="JS/jquery.ui.widget.js"></script>
<script type="text/javascript" src="JS/jquery.ui.position.js"></script>
<script type="text/javascript" src="JS/jquery.ui.autocomplete.js"></script>
<script type="text/javascript">
$(document).ready(function(){
/*
初始化數(shù)據(jù)源
*/
var keys = ["c++", "c#", "c",
"java", "j2ee", "jsp", "javascript", "jquery",
"asp", "asp.net",
"ruby", "vb.net", "visual basic", "vb",
"photo shop", "php",
"flax", "flash"];
function GetValues(key){
var ks = [];
if(key == "") return ks; //如果關(guān)鍵字為空字符串,返回一個空集合
for(var i = 0; i<keys.length; i++){
if(keys[i].indexOf(key) == 0){
ks[ks.length] = keys[i];
}
}
return ks;
}
$('#searchBox').autocomplete({
source : [],
search : function(event,ui){
$('#searchBox').autocomplete(
"option","source",GetValues($(this).val())
);
}
});
});
</script>
<style>
body{padding-top:30px;}
td{ text-align:center; vertical-align:middle; padding:10px;}
#searchBox,#Search{ padding:2px; font-size:15px;}
#searchBox{width:220px;height:17px;}
#Search{width:80px;}
</style>
</head>
<body>
<table border="0" align="center">
<tr>
<td colspan="2"><h1>My Search Engine</h1></td>
</tr>
<tr>
<td><input id="searchBox" /></td>
<td><button id="Search" >Search</button></td>
</tr>
</table>
</body>
</html>
效果圖:

相關(guān)文章
jQuery 源碼分析筆記(5) jQuery.support
接下來是非常糾結(jié)的一個話題,也是所有JS庫必須實現(xiàn)的一個功能:瀏覽器兼容性和為開發(fā)者屏蔽這些差異。2011-06-06輕松掌握jQuery中wrap()與unwrap()函數(shù)的用法
wrap()能夠?qū)⒅付℉TML元素包裹DOM結(jié)構(gòu),與之相反unwrap()函數(shù)則是將DOM去掉^^下面讓我們來以兩個小例子輕松掌握jQuery中wrap()與unwrap()函數(shù)的用法:)2016-05-05jQuery實現(xiàn)的動態(tài)伸縮導(dǎo)航菜單實例
這篇文章主要介紹了jQuery實現(xiàn)的動態(tài)伸縮導(dǎo)航菜單,實例分析了jQuery鼠標(biāo)事件及animate、hide等方法的使用技巧,需要的朋友可以參考下2015-05-05