如何使用jquery中的下一个和上一个功能从页面搜索文本 [英] How to search text from page with next and previous functionality in jquery

查看:73
本文介绍了如何使用jquery中的下一个和上一个功能从页面搜索文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在页面上实现文本搜索功能。我找到了很多链接。但是我需要更多的功能。



这是很好的例子

http://jsfiddle.net/z7fjW/137/ [ ^ ]



I am implementing the Text search functionality on page .I found lot of links .But i need more functionality .

Here is good example
http://jsfiddle.net/z7fjW/137/[^]

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";                             //use body as selector 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="highlight" >"+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;
}





但我需要它只搜索第一个单词(先出现)然后使用下一个进入下一个单词(转到下一个位置)。然后在记事本中的前一个(转到上一个位置)。这可能在查询中吗?



But I need it only search first word(present first) then using next it goes to next (go to next position).Then previous (go to previous position).as in note pad ? Is this possible in query?

推荐答案

(选择器).text()。match(searchTermRegEx);
if(matches){
(selector).text().match(searchTermRegEx); if(matches) {


('.enpated')。removeClass('highlighted'); //删除旧的搜索突出显示
('.highlighted').removeClass('highlighted'); //Remove old search highlights


(选择器).html(
(selector).html(


这篇关于如何使用jquery中的下一个和上一个功能从页面搜索文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆