JQuery search in static HTML page with highlighting of found word

前端 未结 6 802
我在风中等你
我在风中等你 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:44

    Do something like this

     $("p:contains('"+searchedText+"')").each( function( i, element ) {
          var content = $(element).text();
          content = content.replace( searchedText, '' + searchedText + '' );
          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

提交回复
热议问题