【发布时间】:2012-04-12 22:03:19
【问题描述】:
我已经将一个简单的搜索字段放在一起来查看列表,但是我有嵌套列表并且它仅限于单级列表 - 你如何修改
我把它放在了小提琴中; http://jsfiddle.net/marksweb/4CJMe/
如果结果存在,我需要对我的if(filter) 执行什么操作才能同时检查嵌套项并且不隐藏嵌套列表的子项?
演示站点; http://dl.dropbox.com/u/3755926/cssTricks/main.html
(function ($) {
// custom css expression for a case-insensitive contains()
jQuery.expr[':'].Contains = function(a,i,m){
return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
};
function searchList(header, list) { // header is any element, list is an unordered list
// create and add the filter form to the header
var form = $("<form>").attr({"class":"filterform","action":"#"}),
input = $("<input>").attr({"class":"filterinput","type":"text"});
$(form).append(input).prependTo(header);
$(input)
.change( function () {
var filter = $(this).val();
if(filter) {
// this finds all links in a list that contain the input,
// and hide the ones not containing the input while showing the ones that do
$(list).find("a:not(:Contains(" + filter + "))").parent().slideUp();
$(list).find("a:Contains(" + filter + ")").parent().slideDown();
} else {
// return to default
$(list).find("li").slideDown();
}
return false;
})
.keyup( function () {
// fire the above change event after every letter
$(this).change();
});
}
//ondomready
$(function () {
searchList($("#main"), $("#contents-list"));
});
}(jQuery));
【问题讨论】:
-
我最终修复了它(但我不能回答我自己的问题)。将事情提升到查找 li 然后返回查找 ul 已经修复了看起来的问题。