1
我正在做一個小型項目,要求我找到某個詞並在Web瀏覽器中突出顯示該詞。如何找到網頁上的某些詞
簡單地說,我有以下問題:
我想知道是否有標記,以每個單詞在網頁上的位置(X,Y)。
任何人都可以指向我的一些有用的教程在網頁內工作的方向。
我想實現下面的代碼,並讓它與谷歌瀏覽器上的任何網頁進行交互。這是一個簡單的查找和高亮功能。
function doSearch(text) {
if (window.find && window.getSelection) {
document.designMode = "on";
var sel = window.getSelection();
sel.collapse(document.body, 0);
while (window.find(text)) {
document.getElementById("button").blur();
document.execCommand("HiliteColor", false, "lavender");
sel.collapseToEnd();
}
document.designMode = "off";
} else if (document.body.createTextRange) {
var textRange = document.body.createTextRange();
while (textRange.findText(text)) {
textRange.execCommand("BackColor", false, "lavender");
textRange.collapse(false);
}
}
}
謝謝您的時間和精力。
實際涉及的JavaScript看起來很糟糕。這只是錯誤的做法 – Ian