假设你
ul有一个
id的
theList,下面会做的一种方式。
<input type="text" onkeyup="filter(this)" /><script language="javascript" type="text/javascript"> function filter(element) { var value = $(element).val(); $("#theList > li").each(function() { if ($(this).text().search(value) > -1) { $(this).show(); } else { $(this).hide(); } }); }</script>另外,对于基于Marek Tihkan发布的内容的更简洁的版本,您可以将以下内容替换each()循环。不知道这会更好还是更坏。
$('#theList > li:not(:contains(' + value + '))').hide(); $('#theList > li:contains(' + value + ')').show();


