Created
February 28, 2015 05:40
-
-
Save alexhawkins/e8c99a3fb1ad46eaa4d1 to your computer and use it in GitHub Desktop.
Recursive HiLight vs JQuery HiLight
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//RECURSIVE HI-LIGHT | |
function highlight(word) { | |
var matchExp = new RegExp(word, 'g'); | |
var highlightExp = '<span class="highlight">' + word + '</span>'; | |
var traverseDom = function(tree) { | |
for (var i = 0; i < tree.children.length; i++) { | |
var node = tree.children[i]; | |
traverseDom(node); | |
tree.innerHTML = tree.innerHTML.replace(matchExp, highlightExp); | |
} | |
}; | |
traverseDom(document.body); | |
} | |
highlight('Lorem'); | |
//JQUERY HI-LIGHT | |
function highlightJQ(word) { | |
var matchExp = new RegExp(word, 'g'); | |
var highlightExp = '<span class="highlight">' + word + '</span>'; | |
$("*").each(function() { | |
if ($(this).children().length == 0) { | |
$(this).html($(this).html().replace(matchExp, highlightExp)); | |
} | |
}); | |
} | |
highlightJQ('ipsum'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment