DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> jQuery之自動完成組件的深入解析
jQuery之自動完成組件的深入解析
編輯:JQuery特效代碼     

簡單實例
. 代碼如下:
<!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(){
/* 初始化數據源 */
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>

效果圖:

在上述代碼中,在頁面初始化的時候將頁面上的輸入框包裝成jQuery對象,然後使用autocomplete()方法將其包裝成自動完成組件,同時初始化其最小響應長度選項和數據源選項
2:自動完成組件的方法
有Search, Open, Focus, Select, Close, Change事件
. 代碼如下:
function(event, ui) {
//event: 觸發事件時的事件對象
//ui, 是用戶界面對象,ui.item是一個包含label和value屬性的對象
}

. 代碼如下:
<!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(){
/*
初始化數據源
*/
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; //如果關鍵字為空字符串,返回一個空集合

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>

效果圖:


XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved