jQuery之自動(dòng)完成組件的深入解析
簡(jiǎn)單實(shí)例
<!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>
效果圖:

在上述代碼中,在頁(yè)面初始化的時(shí)候?qū)㈨?yè)面上的輸入框包裝成jQuery對(duì)象,然后使用autocomplete()方法將其包裝成自動(dòng)完成組件,同時(shí)初始化其最小響應(yīng)長(zhǎng)度選項(xiàng)和數(shù)據(jù)源選項(xiàng)
2:自動(dòng)完成組件的方法
有Search, Open, Focus, Select, Close, Change事件
function(event, ui) {
//event: 觸發(fā)事件時(shí)的事件對(duì)象
//ui, 是用戶界面對(duì)象,ui.item是一個(gè)包含label和value屬性的對(duì)象
}
<!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)鍵字為空字符串,返回一個(gè)空集合
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>
效果圖:

- JavaScript組件開(kāi)發(fā)完整示例
- 如何使用jquery easyui創(chuàng)建標(biāo)簽組件
- 基于jQuery實(shí)現(xiàn)的美觀星級(jí)評(píng)論打分組件代碼
- 使用jquery組件qrcode生成二維碼及應(yīng)用指南
- jQuery循環(huán)動(dòng)畫與獲取組件尺寸的方法
- jQuery表格排序組件-tablesorter使用示例
- jQuery之排序組件的深入解析
- jQuery之按鈕組件的深入解析
- jQuery之選擇組件的深入解析
- 基于jquery的圖片輪播 tab切換組件
- jQuery進(jìn)行組件開(kāi)發(fā)完整實(shí)例
相關(guān)文章
jQuery 常見(jiàn)開(kāi)發(fā)使用技巧總結(jié)
下面這些知識(shí)都是開(kāi)發(fā)過(guò)程中,經(jīng)常遇到的。所以整理了下,需要的朋友可以參考下。2009-12-12jQuery 源碼分析筆記(5) jQuery.support
接下來(lái)是非常糾結(jié)的一個(gè)話題,也是所有JS庫(kù)必須實(shí)現(xiàn)的一個(gè)功能:瀏覽器兼容性和為開(kāi)發(fā)者屏蔽這些差異。2011-06-06jQuery實(shí)現(xiàn)限制文本框的輸入長(zhǎng)度
本文分享了jQuery實(shí)現(xiàn)限制文本框的輸入長(zhǎng)度的示例代碼,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01輕松掌握jQuery中wrap()與unwrap()函數(shù)的用法
wrap()能夠?qū)⒅付℉TML元素包裹DOM結(jié)構(gòu),與之相反unwrap()函數(shù)則是將DOM去掉^^下面讓我們來(lái)以兩個(gè)小例子輕松掌握jQuery中wrap()與unwrap()函數(shù)的用法:)2016-05-05jQuery實(shí)現(xiàn)的動(dòng)態(tài)伸縮導(dǎo)航菜單實(shí)例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的動(dòng)態(tài)伸縮導(dǎo)航菜單,實(shí)例分析了jQuery鼠標(biāo)事件及animate、hide等方法的使用技巧,需要的朋友可以參考下2015-05-05jQuery實(shí)現(xiàn)優(yōu)雅的彈窗效果(6)
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)優(yōu)雅彈窗效果 的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02