2012-04-14 40 views
1

我已經在使用javascript的uiwebview中設置了一個搜索功能,效果很好,但我希望能夠跳轉到搜索結果中找到的下一個單詞。我已經成功地歌廳視圖通過使用此代碼滾動到第一個實例:在使用Javascript的UIWebView中跳轉搜索結果

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

我怎樣才能得到這個searchresultcount更新到下一發現結果(比如2,3,4,5,等。 ..)當用戶在應用程序中按下按鈕?提前致謝。

回答

0

我能夠用這個JavaScript在web視圖來基本上做同樣的事情:

<script type="text/javascript"> 
var max = 100; 

function goToNext() { 
    var hash = String(document.location.hash); 
    if (hash && hash.indexOf(/hl/)) { 
     var newh = Number(hash.replace("#hl","")); 
     (newh > max-1) ? newh = 0 : void(null); 
     document.location.hash = "#hl" + String(newh-1); 
    } else { 
     document.location.hash = "hl1";   
    } 
} 

</script> 

然後用我的IBAction爲發送此JavaScript調用是這樣的:

- (IBAction)next:(id)sender { 
    [animalDesciption stringByEvaluatingJavaScriptFromString:@"goToNext()"]; 
} 
+0

任何樣品爲這些? – 2013-07-12 11:53:05

1

你的意思是你的應用程序中的原生按鈕,如UIButton?在這種情況下,您可以使用stringByEvaluatingJavaScriptFromString:在您的UIWebView中執行一些JavaScript。你可以做這樣的事情作爲你的按鈕的處理程序:

- (void)buttonPressedAction:(id)sender { 
    NSString * js = @"uiWebview_SearchResultCount++;"; 
    [yourUIWebView stringByEvaluatingJavaScriptFromString: js]; 
} 
+0

這非常像什麼我想要做,但我似乎無法得到它的工作...任何進一步的意見,將不勝感激。感謝您的快速響應! – infobug 2012-04-14 21:26:48

+0

如果沒有更多的細節,很難分辨哪些工作沒有成功。你的按鈕動作設置正確嗎?你可能會張貼更多的代碼嗎? – 2012-04-16 18:37:31

+0

@JakeStoeffler我複製了你的代碼,它不工作,我相信按鈕的行爲是正確的,因爲我寫了一些nslog,你能幫我嗎?我不知道JavaScript,但我可以發佈在這裏的JavaScript代碼,我正在使用 – 2013-07-12 07:55:14

0

用相應的行調用此函數?

function scrollToDesiredHeight(row) { 
    var desiredHeight = span.offsetTop - 140; 
    window.scrollTo(0,row * desiredHeight); 
} 

這是否對你的工作?

+0

我不知道我將如何得到與UIButton的工作? – infobug 2012-04-14 21:31:02

相關問題