下面的代码在 IE 和 Chrome(最新版本)中正常工作
$('#searchBox').keyup(function () {
var searchTxt = $(this).val();
// if item contains searchTxt,
if ($('.item:contains("' + searchTxt + '")')) {
// hide the items that do NOT contain searchTxt
$('.item').not(':contains(' + searchTxt + ')').hide();
};
// capture backspace
if (event.keyCode == 8) {
// show item that contains searchTxt
$('.item:contains(' + searchTxt + ')').show();
};
// if search box is empty,
if ($('#searchBox').val() == "") {
// show all items
$('.item').show();
};
});
上面的代码执行“区分大小写的实时搜索”并且无法执行在 Firefox 中捕获退格键的代码块:
// capture backspace
if (event.keyCode == 8) {
// show item that contains searchTxt
$('.item:contains(' + searchTxt + ')').show();
};