简体   繁体   English

jQuery突出显示所有术语

[英]jQuery highlight all terms

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 "Windows XP" is highlighted but not "Windows". 演示中,您可以看到突出显示了“ Windows XP”,但未突出显示“ Windows”。 What should I do so the both words are highlighted? 我应该怎么做才能使两个单词都突出显示?

 $(document).ready(function() {

          $('p,a').highlight('Windows XP');

});

Thanks for you answers but I think you guys don't understand. 谢谢您的回答,但我想你们听不懂。 The search term is taken from the input field so it changes. 搜索词取自输入字段,因此它会发生变化。 It won't be Windows XP always, it can be "sun on the beach" and I want all words to be highlighted not just "sun" 并非总是Windows XP,它可能是“海滩上的太阳”,我希望所有单词都突出显示,而不仅仅是“太阳”

I updated the demo http://jsfiddle.net/eayan/12/ 我更新了演示http://jsfiddle.net/eayan/12/

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']);

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(' '));

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);

Hopefully you are searching for this: 希望您正在搜索:

 var term = $('#q').val();
 $.each(term.split(" "),function(i,v){
     $('p,a').highlight(v);
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM