我已经使用 javascript 在 uiwebview 中设置了一个搜索,效果很好,但我希望能够跳转到搜索结果中找到的下一个单词。通过使用以下代码,我已成功使视图滚动到第一个实例:
function uiWebview_HighlightAllOccurencesOfStringForElement(element,keyword) {
//declared a var for height
if (element) {
if (element.nodeType == 3) { // Text node
while (true) {
//if (counter < 1) {
var value = element.nodeValue; // Search for keyword in text node
var idx = value.toLowerCase().indexOf(keyword);
if (idx < 0) break; // not found, abort
//(value.split);
//we create a SPAN element for every parts of matched keywords
var span = document.createElement("span");
var text = document.createTextNode(value.substr(idx,keyword.length));
span.appendChild(text);
span.setAttribute("id",keyword);
span.setAttribute("class","uiWebviewHighlight");
span.style.backgroundColor="black";
span.style.color="white";
uiWebview_SearchResultCount++; // update the counter
text = document.createTextNode(value.substr(idx+keyword.length));
element.deleteData(idx, value.length - idx);
var next = element.nextSibling;
element.parentNode.insertBefore(span, next);
element.parentNode.insertBefore(text, next);
element = text;
if(desiredHeight == 0)
{
var offset = {};
offset.x = 0;
offset.y =0;
GetOffset(document.getElementById(keyword),offset);
desiredHeight = offset.y-150;
alert(desiredHeight);
window.scrollTo(0,desiredHeight);
}
}
} else if (element.nodeType == 1) { // Element node
if (element.style.display != "none" && element.nodeName.toLowerCase() !=
'select') {
for (var i=element.childNodes.length-1; i>=0; i--) {
uiWebview_HighlightAllOccurencesOfStringForElement(element.childNodes[i],keyword);
}
}
}
}
}
function GetOffset (object, offset) {
if (!object)
return;
offset.x += object.offsetLeft;
offset.y += object.offsetTop;
GetOffset (object.offsetParent, offset);
}
现在我想在单击下一个按钮时跳转到下一个搜索字符串。我该怎么做?请给我建议。