我用jquery.highlight插件:http://code.google.com/p/gce-empire/source/browse/trunk/jquery.highlight.js?r=2突出重音符號
當我搜索重音單詞,例如「咖啡廳」,它不會突出「咖啡館」比賽...雖然「咖啡廳」的作品很好,但是..所以,我想能夠突出重音的話...任何想法如何做到這一點與此插入?
謝謝!
編輯 好吧,這是工作,但我不得不添加第一之間的空間「(」和最後一個「)」關於「模式」和「patternNoAccents」瓦爾,因爲如果有重音字符在單詞的開始或結尾處,它將不起作用(例如'café')
問題在於它突出顯示單詞AND前後的空格。還有其他解決方案嗎?
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.fn.unhighlight = function (options) {
var settings = { className: 'highlight', element: 'span' };
jQuery.extend(settings, options);
return this.find(settings.element + "." + settings.className).each(function() {
var parent = this.parentNode;
parent.replaceChild(this.firstChild, this);
parent.normalize();
}).end();
};
function stripAccents(str) {
var rExps=[
{re:/[\xC0-\xC6]/g, ch:'A'},
{re:/[\xE0-\xE6]/g, ch:'a'},
{re:/[\xC8-\xCB]/g, ch:'E'},
{re:/[\xE8-\xEB]/g, ch:'e'},
{re:/[\xCC-\xCF]/g, ch:'I'},
{re:/[\xEC-\xEF]/g, ch:'i'},
{re:/[\xD2-\xD6]/g, ch:'O'},
{re:/[\xF2-\xF6]/g, ch:'o'},
{re:/[\xD9-\xDC]/g, ch:'U'},
{re:/[\xF9-\xFC]/g, ch:'u'},
{re:/[\xD1]/g, ch:'N'},
{re:/[\xF1]/g, ch:'n'} ];
for(var i=0, len=rExps.length; i<len; i++)
str=str.replace(rExps[i].re, rExps[i].ch);
return str;
};
jQuery.fn.highlight = function (words, options) {
var settings = { className: 'highlight', element: 'span', caseSensitive: false, wordsOnly: false };
jQuery.extend(settings, options);
if (words.constructor === String) {
words = [words];
}
words = jQuery.grep(words, function(word, i){
return word != '';
});
words = jQuery.map(words, function(word, i) {
return word.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
});
wordsNoAccents = jQuery.map(words, function(word, i) {
return stripAccents(word);
});
if (words.length == 0) { return this; };
var flag = settings.caseSensitive ? "" : "i";
var pattern = "(" + words.join("|") + ")";
if (settings.wordsOnly) {
pattern = "\\b" + pattern + "\\b";
}
var patternNoAccents = "(" + wordsNoAccents.join("|") + ")";
if (settings.wordsOnly) {
patternNoAccents = "\\b" + patternNoAccents + "\\b";
}
var re = new RegExp(pattern, flag);
var reNA = new RegExp(patternNoAccents, flag);
console.log(re);
console.log(reNA);
return this.each(function() {
jQuery.highlight(this, re, settings.element, settings.className);
jQuery.highlight(this, reNA, settings.element, settings.className);
});
};
這個工程太棒了!謝謝! – Santiago 2011-05-23 19:33:40
似乎有人在JavaScript部分粘貼了整個highlight.js – 2014-04-02 12:31:45