jQuery.extend({
highlight: function (node, re, nodeName, className) {
if (node.nodeType === 3) {
var match = node.data.match(re);
if (match) {
var highlight = document.createElement(nodeName || 'span');
highlight.className = className || 'highlight';
var wordNode = node.splitText(match.index);
wordNode.splitText(match[0].length);
var wordClone = wordNode.cloneNode(true);
highlight.appendChild(wordClone);
wordNode.parentNode.replaceChild(highlight, wordNode);
return 1; //skip added node in parent
}
} else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children
!/(script|style)/i.test(node.tagName) && // ignore script and style nodes
!(node.tagName === nodeName.toUpperCase() && node.className === className)) { // skip if already highlighted
for (var i = 0; i < node.childNodes.length; i++) {
i += jQuery.highlight(node.childNodes[i], re, nodeName, className);
}
}
return 0;
}
});
只要這樣調用:jQuery.highlight($("#title"), re, "span", "highlight");
找到一个很棒的 jQuery 插件,可以这样进行高亮:
$('#title').highlight('is');Source
從那個插件中找到如下關鍵代碼
只要這樣調用:
jQuery.highlight($("#title"), re, "span", "highlight");就可以支持正則了。
或者:
https://github.com/bartaz/sandbox.js/blob/master/jquery.highlight.js