2012-02-17 53 views

回答

2

看看您用来突出显示的教程的最后一条评论。 http://zaldzbugz.posterous.com/how-to-search-a-string-inside-uiwebview

所有操作后添加的代码块到UIWebViewSearch.js与跨度

//old code 
    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; 
//new portion of code 
    if (uiWebview_SearchResultCount == 1) 
    { 
     var desiredHeight = span.offsetTop - 140; 
     window.scrollTo(0,desiredHeight); 
    } 

我检查这个解决方案在iPhone模拟器4.3和5.0。

+0

我如何可以滚动到最上面的文本中找到的? – VansFannel 2012-02-17 14:42:54

+0

当uiWebview_SearchResultCount等于1时,这意味着当前跨度突出显示突出显示的文本的第一个出现。 – vrmaks 2012-02-21 09:25:36

5

让我们一步一步来。

亮点

  1. span元素是围绕你搜索的文本添加。对于范围,高亮颜色被设置为范围的背景。修改后的HTML字符串在视图中呈现。

您使用的代码有以下代码片段。

var span = document.createElement("span"); 
     var text = document.createTextNode(value.substr(idx,keyword.length)); 
      span.appendChild(text); 

      span.setAttribute("class","uiWebviewHighlight"); 
      span.style.backgroundColor="black"; 
      span.style.color="white"; 

我们需要一个ID的跨度move.So添加ID如下,

 var span = document.createElement("span"); 
     var text = document.createTextNode(value.substr(idx,keyword.length)); 
      span.appendChild(text); 
      span.setAttribute("id","SEARCH WORD"); 
      span.setAttribute("class","uiWebviewHighlight"); 
      span.style.backgroundColor="black"; 
      span.style.color="white"; 

移动到第一次出现。

在'Webview didload'中使用以下JavaScript移至第一个事件。

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('SEARCH WORD').scrollIntoView()]; 

希望这会有帮助。

1

您可以滚动与此修改中最顶端的文字:

// >=1 
if (uiWebview_SearchResultCount >= 1) 
{ 
    var desiredHeight = span.offsetTop - 140; 
    window.scrollTo(0,desiredHeight); 
}