我最近发现 Resig 的Quicksilver Live Search 的 jQuery 重写并且喜欢它的工作原理......但我正在尝试更新它以使用新的 .on() 事件处理程序。然而,我的能力(或缺乏能力)让我失望。
我想我需要编辑“this.keyup(filter)...”,但我什么也没做。有任何想法吗?
更新:只是澄清一点。我想要更新脚本的想法是接受动态引入的列表项。只要它是一个静态列表,一切都可以正常工作,但是由于我引入了一个通过 ajax 调用创建的列表,所以它无法工作。我将模拟一个小提琴并很快将其链接到此处。与此同时,如果有人能帮助我,它会拯救我的隐藏。谢谢。
jQuery.fn.liveUpdate = function(list){
list = jQuery(list);
if ( list.length ) {
var rows = list.children('li'),
cache = rows.map(function(){
//return this.innerHTML.toLowerCase();
return $('a', this)[0].innerHTML.toLowerCase();
});
this
.keyup(filter).keyup()
.parents('form').submit(function(){
return false;
});
}
return this;
function filter(){
var term = jQuery.trim( jQuery(this).val().toLowerCase() ), scores = [];
if ( !term ) {
rows.show();
} else {
rows.hide();
cache.each(function(i){
var score = this.score(term);
if (score > 0) { scores.push([score, i]); }
});
jQuery.each(scores.sort(function(a, b){return b[0] - a[0];}), function(){
jQuery(rows[ this[1] ]).show();
});
}
}
};