發(fā)布一個(gè)文本自動(dòng)定位組件,可實(shí)現(xiàn)在特定區(qū)域中定位文本并高亮顯示, 用來(lái)解決項(xiàng)目組中機(jī)構(gòu)人員選擇時(shí)人員過(guò)多無(wú)法準(zhǔn)確定位的問(wèn)題。
主要功能為:
1.支持即輸即顯
2.支持模糊查詢
3.支持動(dòng)畫(huà)滾動(dòng)定位到搜索內(nèi)容,當(dāng)有多個(gè)內(nèi)容匹配時(shí),定位到第一個(gè)匹配內(nèi)容
4.支持對(duì)中文字符進(jìn)行搜索
5.使用簡(jiǎn)便,靈活指定樣式,選中樣式在獨(dú)立css中定義
6.輕量級(jí),總共1K
本功能插件基于jquery實(shí)現(xiàn),使用了到j(luò)query.scrollTo插件,主要提供對(duì)特定區(qū)域的文本內(nèi)容的進(jìn)行搜索定位。實(shí)現(xiàn)很簡(jiǎn)單,有注釋,大家一看就明白了的:

js
(function($) {
$.widget("fishout.scrollToText", {
/***********************
* Initialise SuperArea *
***********************/
_init : function() {
var inputDom = $("#" + this.options.inputId);//獲得輸入源
this.element.css("overflow-y", "auto").width(this.options.width).height(this.options.height);//定制搜索區(qū)域
var $this = this;
inputDom.keyup(function() {
var str = inputDom.val();
if ($this.pre) {
$this.pre.removeClass($this.options.selectClass);//清除歷史選中的樣式
}
if (str.gblen() >= $this.options.startMinLen) {//gblen是計(jì)算中文字節(jié)長(zhǎng)度的方法
//獲得本次過(guò)濾的jquery對(duì)象
$this.pre = $this.element.find($this.options.queryDom + ":contains('" + $(this).val() + "')"+($this.options.maxFilter!=-1?':lt('+$this.options.maxFilter+')':''));
$this.pre.addClass($this.options.selectClass);//高亮顯示
$('#contains').stop().scrollTo($this.pre.eq(0), $this.options.delayTime);//自動(dòng)定位
}
});
}});
// Public global variables
$.extend($.fishout.scrollToText, {
version: '0.9',
pre:null,
defaults :{
inputId:'',//過(guò)濾控件id
queryDom:'td',//搜索組件區(qū)域,目前僅支持單個(gè)屬性,有需求會(huì)擴(kuò)展為數(shù)組[]
width:"100%",//控件寬度
height:"100px",//控件高度
delayTime: 1000,//定位動(dòng)畫(huà)時(shí)間
startMinLen: 4,//啟動(dòng)過(guò)濾最小的字符數(shù)
maxFilter: 10,//最大過(guò)濾結(jié)果 -1不限制
selectClass:'selectText'//選中對(duì)象的樣式class
}});
})(jQuery);
組件使用方式如下:

html
<div style="background:#AAE3FF;width:220px;">
過(guò)濾條件:<input type="text" id="qrystr" value=""></div>
<div id="contains" style="border:1px dotted black">
<!--<div id="contains" style="width:220px;height:200px;overflow-y:auto;">-->
<table style="width:200px;height:100%" border=1>
<tr>
<td>
<input type="radio">組織機(jī)構(gòu)1
</td>
<td>
<input type="radio">組織機(jī)構(gòu)2
</td>
</tr>
<tr>
<td>
<input type="radio">組織機(jī)構(gòu)11
</td>
<td>
<input type="radio">組織機(jī)構(gòu)12
</td>
</tr>
</table>
</div>
</body>
<script type='text/javascript'>
$(function() {
$('#contains').scrollToText({
inputId:'qrystr',//過(guò)濾控件id
width:"220px",//控件寬度
height:"300px",//控件高度
delayTime: 1000,//定位動(dòng)畫(huà)時(shí)間
startMinLen: 4,//啟動(dòng)過(guò)濾最小的字符數(shù)
maxFilter: 10,//最大過(guò)濾結(jié)果 當(dāng)=-1不限制
selectClass:'selectText'//符合過(guò)濾條件內(nèi)容class
});
})
</script>
使用截圖如下 :

下載:文本定位插件代碼包
最后說(shuō)幾句題外話:
其實(shí)類似的功能我更傾向于用類似google的自動(dòng)提示來(lái)實(shí)現(xiàn),即使要多選也可以通過(guò)一個(gè)結(jié)果區(qū)域來(lái)存放歷史結(jié)果最后一起提交,不過(guò)該功能為緊急上線,所以就偷懶緊急寫(xiě)了插件給部門(mén)先用起來(lái)了。
Tag標(biāo)簽: jquery