0

我有以下代码来突出显示用户给出的所有单词

 function highlightWord(root, word) {
     textNodesUnder(root).forEach(highlightWords);

     function textNodesUnder(root) {
         var walk = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, null, false),
             text = [],
             node;
         while (node = walk.nextNode()) text.push(node);
         return text;
     }

     function highlightWords(n) {
         for (var i;
             (i = n.nodeValue.indexOf(word, i)) > -1; n = after) {
             var after = n.splitText(i + word.length);
             var highlighted = n.splitText(i);
             var span = document.createElement('span');
             span.className = "spanClass";
             span.style.backgroundColor = "red";
             span.appendChild(highlighted);
             after.parentNode.insertBefore(span, after);
         }
     }
 }

我如何取消突出显示所有单词或当我点击特定单词时?我是编程新手。非常感谢任何帮助

4

0 回答 0