At.js(https://github.com/ichord/At.js)是GitHub上的一个开源项目,MIT授权,主要用来实现类似于微博人人输入@后显示候选好友列表的功能,具体效果可参见官方Demo: http://ichord.github.io/At.js/ 。本文基于版本v0.4.0。
官网上有example.html,但略显复杂,其实At.js的基本使用相当简单,除了作为基础的jquery.js,只需要一个js文件和一个css文件,共三个资源:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>at.js demo</title>
<link rel="stylesheet" type="text/css" href="styles/jquery.atwho.css">
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/jquery.atwho.js"></script>
<script>
$(function(){
$('.inputor').atwho({
at: "@",
data: ["aaa","bbb","ccc"]
});
});
</script>
</head>
<body>
<input id="inputor" class="inputor" name="at" style="width:1000px;height:30px;"/>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>at.js demo</title>
<link rel="stylesheet" type="text/css" href="styles/jquery.atwho.css">
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/jquery.atwho.js"></script>
<script>
$(function(){
var types = ["NUM","CHAR"];
var param1;
var param2;
var commaCount;
$('.inputor').atwho({
at: "(",
data: types,
callbacks: {
/* What to do before insert item's value into inputor.
@param value [String] content to insert
@param $li [jQuery Object] the chosen item */
before_insert: function(value, $li) {
switch(value.substr(1,value.length-1)){
case "NUM":
param1 = [{name: '1'},{name: '2'}];
param2 = [{name: '3'},{name: '4'}];
break;
case "CHAR":
param1 = [{name: 'a'},{name: 'b'}];
param2 = [{name: 'c'},{name: 'd'}];
break;
}
return value;
}
}
});
$('.inputor').atwho({
at: ",",
callbacks: {
/* It would be called to match the `flag`.
It will match at start of line or after whitespace
@param flag [String] current `flag` ("@", etc)
@param subtext [String] Text from start to current caret position.
@return [String | null] Matched result. */
matcher: function(flag, subtext, should_start_with_space) {
var match, regexp;
flag = flag.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
if (should_start_with_space) {
flag = '(?:^|\\s)' + flag;
}
regexp = new RegExp(flag + '([A-Za-z0-9_\+\-]*)$|' + flag + '([^\\x00-\\xff]*)$', 'gi');
match = regexp.exec(subtext);
if (match) {
var i;
commaCount = 0;
for(i=0;i<subtext.length;i++){
if(subtext.charAt(i) == ","){
commaCount++;
}
}
return match[2] || match[1];
} else {
return null;
}
},
/* Filter data by matched string.
@param query [String] Matched string.
@param data [Array] data list
@param search_key [String] key char for seaching.
@return [Array] result data.*/
filter: function(query, data, search_key) {
if(commaCount == 1){
return param1;
}
else if(commaCount == 2){
return param2;
}
}
}
});
});
</script>
</head>
<body>
<input id="inputor" class="inputor" name="at" style="width:1000px;height:30px;" autocomplete = "off"/>
</body>
</html>
新址:http://www.limisky.com/101.html