Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
Created February 28, 2015 05:40
Show Gist options
  • Save alexhawkins/e8c99a3fb1ad46eaa4d1 to your computer and use it in GitHub Desktop.
Save alexhawkins/e8c99a3fb1ad46eaa4d1 to your computer and use it in GitHub Desktop.
Recursive HiLight vs JQuery HiLight
//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