JS will search for keywords to highlight the implementation code

  • 2020-03-26 23:47:20
  • OfStack

Use JS to highlight keywords specified in the article content

Here's the thing...

Now there are these keywords: beauty, life, shopping
When these keywords appear in the article, highlight it..

The article is generated static page, and these keywords can be updated at any time, so I want to use JS to achieve...
Don't know how to implement such function? We inform you for help


<script language="JavaScript">
function highlight(key) {
 var key = key.split('|');
 for (var i=0; i<key.length; i++) {
  var rng = document.body.createTextRange();
  while (rng.findText(key[i]))
  //rng.pasteHTML(rng.text.fontcolor('red'));
  rng.pasteHTML('<div style="border:1 solid red;display:inline"><a href="#" title="+ rng.text +">' + rng.text + '</a></div>');
 }
}
highlight(' The article | The key | function ')
</script>


Related articles: