jQuery highlight all terms

后端 未结 4 1379
南旧
南旧 2021-01-27 15:23

I\'m trying to highlight search terms but it doesn\'t split the words. Two words take as a one string and highlights only the string as is.

In the demo you can see that

相关标签:
4条回答
  • 2021-01-27 15:42

    From the plugin's documentation (available here):

    You can highlight more than one text at once by running highlight with an array of terms as a first attribute. It's much faster than running the highlight function several times.

    $("body p").highlight(["jQuery", "highlight", "plugin"]);

    This means that you can simply use:

    $('p,a').highlight(['Windows XP', 'Windows']);
    
    0 讨论(0)
  • 2021-01-27 15:50

    Try this, it should highlight the whole string and words inside.

    var searchterm = "Windows XP";
    var tohighlight = searchterm.split(" ");
    tohighlight.push(searchterm);
    $("p,a").highlight(tohighlight);
    
    0 讨论(0)
  • 2021-01-27 15:51

    Hopefully you are searching for this:

     var term = $('#q').val();
     $.each(term.split(" "),function(i,v){
         $('p,a').highlight(v);
     });
    
    0 讨论(0)
  • 2021-01-27 15:53

    Supposing you get your string with

    var s = $('#myInput').val();
    

    do

    s.split(' ').forEach(function(token){
        $('p,a').highlight(token);
    });
    

    or

    $("body p").highlight(s.split(' '));
    
    0 讨论(0)
提交回复
热议问题