2011-05-23 59 views
4

我用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); 
      }); 
     }; 

回答

2

看看這個例子。 http://jsfiddle.net/bNPjQ/如果您通過咖啡廳,它將突出咖啡廳和咖啡廳。不確定這是否是所需的功能,但如果您只想突出顯示Café,我可以幫助您。

我從原始插件開始,添加了stripAccents函數,更改了一行,並在高亮函數中添加了對stripAccents的調用。我通過評論標記了所有改變的內容。

當你運行它時,它會做一個沒有單詞的突出顯示,然後2秒鐘後不再高亮顯示,並用wordsOnly做另一個突出顯示。

+0

這個工程太棒了!謝謝! – Santiago 2011-05-23 19:33:40

+0

似乎有人在JavaScript部分粘貼了整個highlight.js – 2014-04-02 12:31:45

0

您必須在文本正文中放入HTML特殊字符代碼。

例如:

Caf&eacute; 

然後在選擇器,用於以下的亮點插件例如:

<a href="javascript:void($('#highlight-plugin').removeHighlight().highlight('Caf&eacute;'));">highlight</a> 

只是測試出來,它突出咖啡館&eacute的所有實例;在文件中。

+0

的問題是,我用它來突出顯示搜索結果很多選擇,我在UTF-8顯示結果,與人物就像「咖啡館」。 – Santiago 2011-05-23 04:50:34

+0

您是否嘗試投入.highlight('Café')? – ectype 2011-05-23 04:57:43

+0

@ectype,只是嘗試了harcoded函數.highlight('café');並沒有工作。 .highlight('cafe');工作得很好。它必須是關於jquery.highlight插件的東西,當它搜索DOM來添加「高亮」類或其他東西時。 – Santiago 2011-05-23 05:01:52