我在Javascript中很新。 我使用方法在当前页面中查找文本并跳转到该位置。
这是我正在使用的Example。
该示例在IE 10/11,Chrome和FireFox上运行良好,但是一旦我尝试在Microsoft Edge上使用它,就什么都不会发生。我使用了调试器,发现window.find
和window.document.body.createTextRange
是undefined
。
我无法找到任何暗示Edge不支持此功能的提示。 https://msdn.microsoft.com/en-us/library/office/aa207181(v=office.11).aspx
这是我的代码
var n = 0;
function FindText(str) {
var txt;
var i;
var found;
//Empty String
if (str == "") {
return false;
}
// Find next occurance of the given string on the page, wrap around to the
// start of the page if necessary.
if (window.find) {
// Look for match starting at the current point. If not found, rewind
// back to the first match.
if (!window.find(str)) {
while (window.find(str, false, true)) {
n++;
}
} else {
n++;
}
// If not found in either direction, give message.
if (n == 0) {
alert("Not found.");
}
} else if (window.document.body.createTextRange) {
txt = window.document.body.createTextRange();
// Find the nth match from the top of the page.
found = true;
i = 0;
while (found === true && i <= n) {
found = txt.findText(str);
if (found) {
txt.moveStart("character", 1);
txt.moveEnd("textedit");
}
i += 1;
}
// If found, mark it and scroll it into view.
if (found) {
txt.moveStart("character", -1);
txt.findText(str);
txt.select();
txt.scrollIntoView();
n++;
} else {
// Otherwise, start over at the top of the page and find first match.
if (n > 0) {
n = 0;
findInPage(str);
}
// Not found anywhere, give message. else
alert("Not found.");
}
}
return false;
}