取消选中所选文本的高亮显示

时间:2013-11-08 12:21:31

标签: javascript html css

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

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

我如何取消所有单词或者当我点击部分单词时?是一种新的编程。非常感谢任何帮助

0 个答案:

没有答案