我在 Jquery 脚本上获得了一些帮助,该脚本创建了可搜索、可切换的常见问题解答。代码可以在这里看到:
http://jsfiddle.net/pT6dB/62/ http://jsfiddle.net/pT6dB/62/
问题是,如果单词“How”带有大写“H”,我搜索“h”,它不会找到它。如何使该脚本不区分大小写?
Update
或者,您可以使用正则表达式显着减少代码量。jsFiddle 演示 http://jsfiddle.net/GeekyEggo/pT6dB/72/
$('#search').keyup(function(e) {
// create the regular expression
var regEx = new RegExp($.map($(this).val().trim().split(' '), function(v) {
return '(?=.*?' + v + ')';
}).join(''), 'i');
// select all list items, hide and filter by the regex then show
$('#result li').hide().filter(function() {
return regEx.exec($(this).text());
}).show();
});
Original
根据您当前确定相对元素的算法,您可以使用 jQueryfilter
方法来过滤你的结果keywords
大批。这是一个粗略的想法:
// select the keywords as an array of lower case strings
var keywords = $(this).val().trim().toLowerCase().split(' ');
// select all list items, hide and filter then show
$('#result li').hide().filter(function() {
// get the lower case text for the list element
var text = $(this).text().toLowerCase();
// determine if any keyword matches, return true on first success
for (var i = 0; i < keywords.length; i++) {
if (text.indexOf(keywords[i]) >= 0) {
return true;
}
}
}).show();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)