我要突出显示页面上的文本,如果它与数组中的文本匹配。目的是突出显示页面上的重复项。
var iterator = document.evaluate('xpathgoeshere', document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
try {
var thisNode = iterator.iterateNext();
var arrayList = [];
while (thisNode) {
arrayList.push(thisNode.textContent);
thisNode = iterator.iterateNext();
}
console.log(arrayList);
for (var i = 0; i < arrayList.length; i++) {
console.log(arrayList[i]);
}
} catch (e) {
dump('Error: Document tree modified during iteration ' + e);
}
var arrayListDupes = arrayList.slice().sort();
var results = [];
for (var i = 0; i < arrayListDupes.length - 1; i++) {
if (arrayListDupes[i + 1] == arrayListDupes[i]) {
//while (results) {
results.style.outline = "5px dashed red"; // I am probably doing
results = iterator.iterateNext(); // it completely incorrect
results.push(arrayListDupes[i]);
}
}
console.log(results);
答案 0 :(得分:1)
通过以下功能使用现有代码。
var TRange=null;
function findString (str) {
if (parseInt(navigator.appVersion)<4) return;
var strFound;
if (window.find) {
// CODE FOR BROWSERS THAT SUPPORT window.find
strFound=self.find(str);
if (!strFound) {
strFound=self.find(str,0,1);
while (self.find(str,0,1)) continue;
}
}
else if (navigator.appName.indexOf("Microsoft")!=-1) {
// EXPLORER-SPECIFIC CODE
if (TRange!=null) {
TRange.collapse(false);
strFound=TRange.findText(str);
if (strFound) TRange.select();
}
if (TRange==null || strFound==0) {
TRange=self.document.body.createTextRange();
strFound=TRange.findText(str);
if (strFound) TRange.select();
}
}
else if (navigator.appName=="Opera") {
alert ("Opera browsers not supported, sorry...")
return;
}
if (!strFound) alert ("String '"+str+"' not found!")
return;
}
尝试像findString (results[0])
一样呼叫,文本应在页面中突出显示。
findString()
源和演示here