简体   繁体   English

使用跨度动态突出显示div中的多个单词

[英]Dynamically highlight multiple words inside a div using spans

I want to highlight (or "annotate") several words inside a div, using spans with a background color. 我想使用具有背景颜色的跨度来突出显示(或“注释”)div中的几个单词。

In my (minimal) example below, the words "x" and "y" should be annotated (position of y is 37 and of x is 44). 在下面的(最小)示例中,应标注单词“ x”和“ y”(y的位置为37,x的位置为44)。 However, my method "annotateString" does not seem to work, but I don't know why... 但是,我的方法“ annotateString”似乎不起作用,但是我不知道为什么...

The idea of my method is to iterate over the div html content to add the span prefix or suffix at the needed positions. 我的方法的想法是遍历div html内容,以在所需位置添加span前缀或后缀。 Because of the fact, that I give the positions based on the string and not on the perhaps due to prior annotations modified html content, I want the position pointer "pos" to freeze if its inside a html element (f.ex. already existing spans). 由于这样的事实,我基于字符串而不是基于先前的注释修改的html内容给出位置,所以我希望位置指针“ pos”在其位于html元素(例如f.ex.已经存在)中冻结时跨度)。

 function annotateString(str, start, end, prefix, suffix) { var annotatedString = ''; var pos = -1; var freeze = false; if (str[0] == '<') { freeze = true; } for (var i = 0; i < str.length; i++) { if (!freeze && str[i] != '<') pos++; if (str[i] == '<') freeze = true; if (str[i] == '>') freeze = false; if (pos == start && !freeze) annotatedString += prefix + str[i]; else if (pos + 1 == end && !freeze) annotatedString += str[i] + suffix; else annotatedString += str[i]; } return annotatedString; } $(function() { var strOld = $("#item").html(); var prefix = '<span style="background-color:yellow;">'; var suffix = '</span>'; // this should annotate the "y" var strNew = annotateString(strOld, 37, 37, prefix, suffix); $("#item").html(strNew); // this should annotate the "x" var strOld = $("#item").html(); var strNew = annotateString(strOld, 44, 44, prefix, suffix); $("#item").html(strNew); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="item">In this string I want to annotate "y" <- "x"</div> 

There are three errors I had to cop with 我不得不应对三个错误

  • your html contain a < , that I replaced with &lt; 您的html包含< ,我用&lt;替换了
  • the positions where a little bit odd. 有点奇怪的位置。
  • I'm not sure I got your if (pos + 1 == end) so I ended up with another solution, I think code is self explanatory! 我不确定我if (pos + 1 == end)知道您的if (pos + 1 == end)所以我得出了另一个解决方案,我认为代码是可以自我解释的!

 function annotateString(str, start, end, prefix, suffix) { var annotatedString = ''; var pos = -1; var freeze = false; if (str[0] == '<') { freeze = true; } for (var i = 0; i < str.length; i++) { if (!freeze && str[i] != '<') pos++; if (str[i] == '<') freeze = true; if (str[i] == '>') freeze = false; // -------------------------------------- prefix on start if (pos == start && !freeze) annotatedString += prefix; // --------------------------------------- content anyway annotatedString += str[i]; // ---------------------------------------- suffix on end if (pos == end && !freeze) annotatedString += suffix; } return annotatedString; } $(function() { var strOld = $("#item").html(); var prefix = '<span style="background-color:yellow;">'; var suffix = '</span>'; // this should annotate the "y" var strNew = annotateString(strOld, 35, 35, prefix, suffix); $("#item").html(strNew); // this should annotate the "x" strOld = $("#item").html(); strNew = annotateString(strOld, 45, 45, prefix, suffix); $("#item").html(strNew); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="item">In this string I want to annotate "y" &lt;- "x"</div> 

Also your initialisation of freeze variable to true is not necessary since you double check for < character, but that's just code review 另外,将freeze变量初始化为true也是不必要的,因为您仔细检查了<字符,但这只是代码检查

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

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