javascript - 搜索选项不适用于小写和大写

标签 javascript jquery html

我在我的 jsfiddle 中做了一个搜索选项:http://jsfiddle.net/alonshmiel/46wyH/33/

所以我有一个值(value 是搜索到的文本)。

我搜索所有包含值的 li:value

// hide all the lists
$('#searchExcludeResult li').each(function() {       
      $(this)[0].style.display = "none";
});

// show all the li that contains the value: value
$('#searchExcludeResult li:contains(' + value + ')').each(function() {       
    $(this)[0].style.display = "list-item";
    $(this)[0].innerHTML = $(this)[0].textContent.replace(value, 
                                '<span style="font-weight: bold">' + value + '</span>');
});

它可以工作,但无论搜索的文本是小写还是大写,我都希望它能工作。

例如:

<ul>
    <li>United States</li>
    <li>London, England</li>
    <li>Moskow, Russia</li>
</ul>

搜索到的文本是:e,显示第一个(unit e d stat e s)和第二个li(London, 英国)。

搜索到的文本可以是EN, eN, l, L

感谢任何帮助!

最佳答案

Fiddle Demo

使用.filter()并匹配.toLowerCase() .text().

$('#searchExcludeResult li').filter(function () {
    return $(this).text().toLowerCase().indexOf(value.toLowerCase()) !== -1;
}).each(function () {

关于javascript - 搜索选项不适用于小写和大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21967014/

相关文章:

javascript - 使用scale属性调整svg元素的大小

javascript - ui-router - $state.go() 不工作

jquery - show/hide div 导致内容跳起来

jquery - Jquery Ajax POST 中出现 400 bad request 错误

php - Jquery 在表单提交之间隐藏/显示

html - 文本容器大小 - 文本换行?

html - SVG 引用外部模式 ID

javascript - 删除图像标签属性的所有实例

javascript - Bootstrap 4 工具提示未按样式显示。而且它仅适用于 anchor 标记

javascript - 如何在 Firefox 中禁用视频的单击和双击控件?