2012-02-17 58 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); 
}