【问题标题】:I have a issue with jquery search, hightlight and jump to the highlighted code我对 jquery 搜索有疑问,突出显示并跳转到突出显示的代码
【发布时间】:2014-02-04 10:50:27
【问题描述】:

我正在使用来自this site 的代码 还有running jsfiddle。 我注意到,当您在小提琴示例中搜索时,您输入的单词将替换为实际结果,我试图将其删除,因为我只需要查找并突出显示。如果您搜索“”,我也会遇到错误,我正在尝试通过调整代码来解决它,但老实说,我不了解整个过程。非常欢迎任何帮助。

function searchAndHighlight(searchTerm, selector) {
if(searchTerm) {
    //var wholeWordOnly = new RegExp("\\g"+searchTerm+"\\g","ig"); //matches whole word only
    //var anyCharacter = new RegExp("\\g["+searchTerm+"]\\g","ig"); //matches any word with any of search chars characters
    var selector = selector || "body";                             //default selector is body if none provided
    var searchTermRegEx = new RegExp(searchTerm,"ig");
    var matches = $(selector).text().match(searchTermRegEx);
    if(matches) {
        $('.highlighted').removeClass('highlighted');     //Remove old search highlights
        $(selector).html($(selector).html()
                .replace(searchTermRegEx, "<span class='highlighted'>"+searchTerm+"</span>"));
        if($('.highlighted:first').length) {             //if match found, scroll to where the first one appears
            $(window).scrollTop($('.highlighted:first').position().top);
        }
        return true;
    }
}
return false;
}

    $(document).ready(function() {
        $('#search-button').on("click",function() {
            if(!searchAndHighlight($('#search-term').val(), "#bass")) {
                alert("No hay resultados");
            }
        });
    });

【问题讨论】:

    标签: jquery search replace highlight


    【解决方案1】:

    替换

    var selector = selector || "body"; 
    

     var selector = selector || "#bodyContainer";
    

    解释:

    问题在于您的 regexbody tag 中的值匹配,即它还试图匹配 children tags 的值,例如 '&lt;br&gt;' 中的 'b' 。

    预期的行为是仅在包含数据的container 内搜索,即bodyContainer

    编辑:基于 cmets。

    那是因为regex is ignoring case for matching 和您的replace function replaces the found value with the query string。因此,如果找到的值为 EMPTY 且查询字符串为 eMpTy,则您的正则表达式将匹配它,并且您的替换函数将用查询文本替换实际值(以及添加跨度)

    【讨论】:

    • 嗨,我尝试了你所说的并将选择器更改为#bodyContainer 并且它仍然有效,我在小提琴示例中的问题是,如果在搜索框中输入“空”,那么第一个结果如果是“空”,将替换为结果“空”,我仍然无法使其工作。
    【解决方案2】:

    gaurav5430 说你做的是对的。您正在使用搜索词并将其替换为实际字母。您需要做的是获取捕获术语并突出显示它。我很确定有更有效的方法,但我快速而肮脏的方法是在 if(matches) 情况下

            $('.highlighted').removeClass('highlighted');       //Remove old search highlights
            var index;
            for (index = 0; index < matches.length; ++index) {  //Go through all the matched cases 
                var wordreg = new RegExp(matches[index]);       //get a new regex that has the exact casing and wording
                $(selector).html($(selector).html()             //then find that and replace it with right casings
                    .replace(wordreg, "<span class='highlighted'>"+matches[index] +"</span>"));
            }
    
            if($('.highlighted:first').length) {             //if match found, scroll to where the first one appears
                $(window).scrollTop($('.highlighted:first').position().top);
            }
            return true;
    

    它很丑,但它有效。这是 jsFiddle http://jsfiddle.net/z7fjW/265/

    **编辑

    当您对文本进行正则表达式时,您会得到一个匹配的字符串数组。例如:你在 'Hey hey heEY heY' 中搜索 'Hey',你会得到一个字符串数组 ['Hey', 'hey', 'hEY', 'heY']。

    您必须遍历此数组,找到与不忽略大小写的正则表达式对应(匹配)的字符串,然后用突出显示的字符串替换。查看我代码中的 for 循环。

    【讨论】:

    • 你也应该遵循 gaurav5430 的建议,它会搜索整个身体,所以如果你搜索“搜索”,搜索栏就会被替换
    • 谢谢,我已经添加并且工作正常,但我注意到现在只在所有文档中选择一个结果,我该如何修改才能找到所有结果?
    • 好的,现在我阅读了有关正则表达式并将修饰符添加到“ig”并解决多个单词的突出显示,现在我该如何解决消息:“此页面上的脚本可能很忙,或者它可能已停止响应”,我在此站点上阅读了一些答案,并说可能存在一个循环,我检查了代码,根据我的理解,这很好,我看不出应该是什么繁重的任务:/
    • 你能把你现在拥有的东西弄乱吗?
    • 当然,我最终得到以下代码:jsfiddle.net/gabouh075/U7nYt 它工作正常,但在我的替换部分的特定项目中,我不得不使用 match[1],因为我得到了一个不同的数组八要素。我真的不知道为什么,我在我正在搜索的一大堆文本中进行了一些测试,如果我在完整的八个元素中尝试“奥斯卡”这个词包含:[“osCar”,“奥斯卡”,“ Oscar”、“Oscar”、“Oscar”、“Oscar”、“Oscar”、“Oscar”] 在 Chrome 中也有问题,它可以找到文本并跳到窗口的下部
    猜你喜欢
    • 2011-05-14
    • 2015-10-14
    • 1970-01-01
    • 2013-11-12
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多