JQuery search in static HTML page with highlighting of found word

前端 未结 6 804
我在风中等你
我在风中等你 2020-12-02 19:11

I\'ve been trying to make a simple search inside a static HTML page using JQuery. I have to mention that this is just my first time working with JQuery.

I\'m trying

6条回答
  •  醉话见心
    2020-12-02 19:22

    Here is mine: http://jsfiddle.net/x8rpY/1/

    JS:

    $('#searchfor').keyup(function(){
             var page = $('#all_text');
             var pageText = page.text().replace("","").replace("");
             var searchedText = $('#searchfor').val();
             var theRegEx = new RegExp("("+searchedText+")", "igm");    
             var newHtml = pageText.replace(theRegEx ,"$1");
             page.html(newHtml);
        });
    

    CSS:

    #all_text span
    {
        text-decoration:underline;
        background-color:yellow;    
    }
    

    Works for repeated search also.

提交回复
热议问题