5 Highlights arbitrary terms.
7 <http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>
12 <http://johannburkard.de>
17 jQuery.fn.highlight = function(pat) {
18 function innerHighlight(node, pat) {
20 if (node.nodeType == 3) {
21 var pos = node.data.toUpperCase().indexOf(pat);
23 var spannode = document.createElement('span');
24 spannode.className = 'term';
25 var middlebit = node.splitText(pos);
26 var endbit = middlebit.splitText(pat.length);
27 var middleclone = middlebit.cloneNode(true);
28 spannode.appendChild(middleclone);
29 middlebit.parentNode.replaceChild(spannode, middlebit);
33 else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
34 for (var i = 0; i < node.childNodes.length; ++i) {
35 i += innerHighlight(node.childNodes[i], pat);
40 return this.each(function() {
41 innerHighlight(this, pat.toUpperCase());
45 jQuery.fn.removeHighlight = function() {
46 return this.find("span.term").each(function() {
47 this.parentNode.firstChild.nodeName;
48 with (this.parentNode) {
49 replaceChild(this.firstChild, this);