2014-04-03 33 views
0

我正在使用函數here,我的問題在於,當我搜索多個段落時,由於某種奇怪的原因,var = MyApp_SearchResultCount不連續計數。當我一個文字塊上運行腳本,它可以作爲我本來期望,但在多個段落搜索時,我得到這樣的:突出顯示字符串的所有出現次數 - 未正確遞增

<p> 
text text [id=4]search string[/] text text text text [id=5]search string[/] text 
text text text [id=6]search string[/] text text 
</p> 
<p> 
text text [id=3]search string[/] text text text text 
text text text text text 
</p> 
<p> 
text text text text text text [id=1]search string[/] text 
text text text [id=2]search string[/] text text 
</p> 

沒有任何人有任何想法,爲什麼發生這種情況以及如何補救它?

工作jsFiddle

+0

我的回答對你有幫助嗎? – smerny

+0

對不起,我錯過了你的答案,修正了它!謝謝! –

回答

1

的原因是因爲它是通過反向孩子迭代...試試這個:

JSFiddle

我改變的是這裏的一部分(在第32行開始):

} else if (element.nodeType == 1) { // Element node 
     if (element.style.display != "none" && element.nodeName.toLowerCase() != 'select') { 
      $(element).contents().each(function() { 
       MyApp_HighlightAllOccurencesOfStringForElement(this,keyword); 
      }); 
     } 
    } 
相關問題