使用Javascript在UIWebView中跳转搜索结果 [英] Jump through search results in UIWebView with Javascript

查看:95
本文介绍了使用Javascript在UIWebView中跳转搜索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在uiwebview中使用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. 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);
}

如何将此searchresultcount更新为下一个找到的结果(例如2 ,3,4,5,等...)当用户在app中按下按钮?在此先感谢。

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?? Thanks in advance.

推荐答案

我能够在网页浏览中使用此javascript做同样的事情:

I was able to do basically the same thing with this javascript in the webview:

<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调用:

Then sending this JavaScript call with my IBAction like this:

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

这篇关于使用Javascript在UIWebView中跳转搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆