首页 > 解决方案 > 突出显示搜索的关键字

问题描述

我正在突出显示我搜索的关键字 - 但是它会搜索 HTML,所以如果我有类似的东西

<a href="espn.com?id=456">espn</a> 

并在 456 上搜索 - 它找到了 - 我不想要那个。

这就是我正在使用的

function highlightOnly(text) {
    $('#' + view + ' td').each(function(i, e) {
        var $e = $(e);
        $e.html($(e).html().split(text).join('<span class="matching">' + text + '</span>'));
    })
}

$('.matching').replaceWith(function() {
    return this.innerText;
});

highlightOnly($('#txtSearch').val().toUpperCase());

你可以在这里看到原始小提琴 搜索关键字突出显示

标签: javascriptjquery

解决方案


推荐阅读