简体   繁体   English

使用Javascript在UIWebView中浏览搜索结果

[英]Jump through search results in UIWebView with Javascript

I have set up a search in uiwebview with javascript that works great, but I want to be able to jump to the next found word in the search results. 我已经在uiwebview中使用javascript进行了很好的搜索,但是我希望能够跳转到搜索结果中的下一个找到的单词。 I have succeeded in geting the view to scroll to the first instance by using this code: 通过使用以下代码,我已经成功地将视图滚动到了第一个实例:

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

How can I get this searchresultcount to update to the next found result(say 2, 3, 4, 5, ect...) when user presses button in app?? 当用户在应用程序中按下按钮时,如何获取此searchresultcount来更新到下一个找到的结果(例如2、3、4、5等)? Thanks in advance. 提前致谢。

Do you mean a native button in your app such as a UIButton? 您是指应用程序中的本机按钮(例如UIButton)吗? In that case, you can use stringByEvaluatingJavaScriptFromString: to execute some JavaScript in your UIWebView. 在这种情况下,您可以使用stringByEvaluatingJavaScriptFromString:在UIWebView中执行一些JavaScript。 You could do something like this as the handler for your button: 您可以将以下操作用作按钮的处理程序:

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

call this function with the appropriate row? 用适当的行调用此函数?

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

Does this work for you? 这对您有用吗?

I was able to do basically the same thing with this javascript in the webview: 我可以使用此javascript在网络视图中执行基本相同的操作:

<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>

Then sending this JavaScript call with my IBAction like this: 然后像下面这样用我的IBAction发送此JavaScript调用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM