JQuery search in static HTML page with highlighting of found word

前端 未结 6 780
我在风中等你
我在风中等你 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("<span>","").replace("</span>");
             var searchedText = $('#searchfor').val();
             var theRegEx = new RegExp("("+searchedText+")", "igm");    
             var newHtml = pageText.replace(theRegEx ,"<span>$1</span>");
             page.html(newHtml);
        });
    

    CSS:

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

    Works for repeated search also.

    0 讨论(0)
  • 2020-12-02 19:24

    Here is another example that I quickly hacked: http://jsfiddle.net/VCJUX/

    0 讨论(0)
  • 2020-12-02 19:28

    Why using a selfmade highlighting function is a bad idea

    The reason why it's probably a bad idea to start building your own highlighting function from scratch is because you will certainly run into issues that others have already solved. Challenges:

    • You would need to remove text nodes with HTML elements to highlight your matches without destroying DOM events and triggering DOM regeneration over and over again (which would be the case with e.g. innerHTML)
    • If you want to remove highlighted elements you would have to remove HTML elements with their content and also have to combine the splitted text-nodes for further searches. This is necessary because every highlighter plugin searches inside text nodes for matches and if your keywords will be splitted into several text nodes they will not being found.
    • You would also need to build tests to make sure your plugin works in situations which you have not thought about. And I'm talking about cross-browser tests!

    Sounds complicated? If you want some features like ignoring some elements from highlighting, diacritics mapping, synonyms mapping, search inside iframes, separated word search, etc. this becomes more and more complicated.

    Use an existing plugin

    When using an existing, well implemented plugin, you don't have to worry about above named things. The article 10 jQuery text highlighter plugins on Sitepoint compares popular highlighter plugins. This includes plugins of answers from this question.

    Have a look at mark.js

    mark.js is such a plugin that is written in pure JavaScript, but is also available as jQuery plugin. It was developed to offer more opportunities than the other plugins with options to:

    • search for keywords separately instead of the complete term
    • map diacritics (For example if "justo" should also match "justò")
    • ignore matches inside custom elements
    • use custom highlighting element
    • use custom highlighting class
    • map custom synonyms
    • search also inside iframes
    • receive not found terms

    DEMO

    Alternatively you can see this fiddle.

    Usage example:

    // Highlight "keyword" in the specified context
    $(".context").mark("keyword");
    
    // Highlight the custom regular expression in the specified context
    $(".context").markRegExp(/Lorem/gmi);
    

    It's free and developed open-source on GitHub (project reference).

    Example of mark.js keyword highlighting with your code

    $(function() {
      $("input").on("input.highlight", function() {
        // Determine specified search term
        var searchTerm = $(this).val();
        // Highlight search term inside a specific context
        $("#context").unmark().mark(searchTerm);
      }).trigger("input.highlight").focus();
    });
    mark {
      background: orange;
      color: black;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/mark.js/7.0.0/jquery.mark.min.js"></script>
    <input type="text" value="test">
    <div id="context">
      Lorem ipsum dolor test sit amet
    </div>

    0 讨论(0)
  • 2020-12-02 19:29

    (for one thing you want to use Background-Color, not Color, for background)

    I would create a css class for normal and a seperate (inherited) css class for highlighted text, and then use the JQuery to change the css class when you find what you are looking for.

    Just my initial thoughts though, not sure if there is a better way of doing it.

    EDIT: if you want to change only a specific word, you'll have to modify innerHTML to put it in a seperate tag at that point.

    0 讨论(0)
  • 2020-12-02 19:44

    Do something like this

     $("p:contains('"+searchedText+"')").each( function( i, element ) {
          var content = $(element).text();
          content = content.replace( searchedText, '<span class="search-found">' + searchedText + '</span>' );
          element.html( content );
     });
    
     .search-found {
         text-decoration: underline;
     }
    

    p.s. this will work only if each of the "elements" has plain text only content otherwise it would remove children nodes

    EDIT: removed the extra ')' in the each callback

    0 讨论(0)
  • 2020-12-02 19:46

    $(function() {
      $("input").on("input.highlight", function() {
        // Determine specified search term
        var searchTerm = $(this).val();
        // Highlight search term inside a specific context
        $("#context").unmark().mark(searchTerm);
      }).trigger("input.highlight").focus();
    });
    mark {
      background: orange;
      color: black;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/mark.js/7.0.0/jquery.mark.min.js"></script>
    <input type="text" value="test">
    <div id="context">
      Lorem ipsum dolor test sit amet
    </div>

    0 讨论(0)
提交回复
热议问题