2012-02-25 13 views
4

例如,如果我有一個網頁HTML像下面匹配所有出現的window.find()

<body> 
     Hello Techies, <br> 
     Techies here. 
</body> 

如果我搜索「技術高手」使用

var sel = window.getSelection(); 
sel.collapse(document.body, 0); 
document.body.offsetHeight; 
if (window.find("Techies", true)) { 
    document.execCommand("hiliteColor", false, "YellowGreen"); 
    sel.collapseToEnd(); 
} 

它只有突出首先發生的「技術」。但是當我使用Ctrl + F搜索時,第一個事件將在黑暗中突出顯示,而下一個事件將在淺色模式下突出顯示。我怎樣才能達到與上面的代碼相同。

回答

6

嘗試使用while循環:

if (window.find("Techies", true)) { 
    document.execCommand("hiliteColor", false, "FirstColor"); 
    while (window.find("Techies", true)) { 
     document.execCommand("hiliteColor", false, "SecondColor"); 
    } 
    ... 
} 
+0

作品:-)非常感謝。 – Exception 2012-02-25 20:09:22

相關問題