我覺得這個棘手的部分實際上是智能地接受用戶輸入。因此,我認爲最好的做法是將搜索傳遞給自動完成類型的插件。一旦頁面準備就緒,您將焦點傳遞給輸入文本框,然後讓插件在您搜索時執行其魔術...
例如,您可以使用quicksearch插件。
然後給出的數據表和輸入這樣的:
<input id="searcher" type="text" name="searcher">
你可以有一個現成的功能,看起來像這樣:
$('#searcher').quicksearch('table tbody tr', {
'delay': 100,
'bind': 'keyup keydown',
'show': function() {
if ($('#searcher').val() === '') {
return;
}
$(this).addClass('show');
},
'onAfter': function() {
if ($('#searcher').val() === '') {
return;
}
if ($('.show:first').length > 0){
$('html,body').scrollTop($('.show:first').offset().top);
}
},
'hide': function() {
$(this).removeClass('show');
},
'prepareQuery': function(val) {
return new RegExp(val, "i");
},
'testQuery': function(query, txt, _row) {
return query.test(txt);
}
});
$('#searcher').focus();
嘗試一下這裏:http://jsfiddle.net/ZLhAd/369/
編輯:添加其他答案/評論以使輸入固定並停止scollbar如此荒唐地跳躍。
哇,這個石頭!謝謝! – 2010-12-01 19:55:47
由於github不是一個正確的cdn,它不能得到js文件加載的問題上的小提琴被打破..固定它與 http://jsfiddle.net/ZLhAd/205/ – Kender 2015-08-06 13:29:19