UIWebView 下一次出现来自搜索问题的单词

Posted

技术标签:

【中文标题】UIWebView 下一次出现来自搜索问题的单词【英文标题】:UIWebView next occurence of a word froma search issue 【发布时间】:2015-02-13 22:46:59 【问题描述】:

所以我有一个简单的 UIWEBVIEW 应用程序,它在 UIWEBVIEW 中显示 html。从 Webview ,我有一个连接到我的 xcode 项目的 javascript 应用程序,并在用户输入并单击“Net”按钮时突出显示所有选定的单词。话虽如此,我试图让“下一个” UIButton 向下滚动到我的应用程序中下一个出现的突出显示的单词。这是javascript部分:

UIWEBVIEWSEARCH.JS

var uiWebview_SearchResultCount;
var currSelected;
var currentHighlightedSpan;


function uiWebview_HighlightAllOccurencesOfStringForElement(element,keyword) 

    if (element) 
        if (element.nodeType == 3)         // Text node
            while (true) 
                //if (counter < 1) 
                var value = element.nodeValue;  // Search for keyword in text node
                var idx = value.toLowerCase().indexOf(keyword);

                if (idx < 0) break;             // not found, abort

                //(value.split);

                //we create a SPAN element for every parts of matched keywords
                var span = document.createElement("span");
                var text = document.createTextNode(value.substr(idx,keyword.length));
                span.appendChild(text);

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


                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;
                uiWebview_SearchResultCount++;    // update the counter


                //this is code
              //  if (uiWebview_SearchResultCount == 1)
              //  
              //      var desiredHeight = span.offsetTop - 140;
             //       window.scrollTo(0,desiredHeight);
            //    

            
         else if (element.nodeType == 1)  // Element node
            if (element.style.display != "none" && element.nodeName.toLowerCase() != 'select') 
                for (var i=element.childNodes.length-1; i>=0; i--) 
                    uiWebview_HighlightAllOccurencesOfStringForElement(element.childNodes[i],keyword);
                
            
        
    

添加了代码 UIWEBVIEWSEARCH.JS 以查找下一个出现的字符串

function goNext()
    jump(1);

function goPrev()
    jump(-1);


function jump(howHigh)
    prevSelected = currSelected;
    currSelected = currSelected + howHigh;

    if (currSelected < 0)
        currSelected = MyApp_SearchResultCount + currSelected;
    
    if (currSelected >= MyApp_SearchResultCount)
        currSelected = currSelected - MyApp_SearchResultCount;
    

    prevEl = document.getElementsByClassName("uiWebviewHighlight")[prevSelected];
    if (prevEl)
        prevEl.style.backgroundColor="yellow";
    
    el = document.getElementsByClassName("uiWebviewHighlight")[currSelected];
    el.style.backgroundColor="green";
    el.scrollIntoView(true); //thanks techfoobar

XCODE - SearchWebView.M

@implementation SearchWebView

- (NSInteger)highlightAllOccurencesOfString:(NSString*)str

    NSString *path = [[NSBundle mainBundle] pathForResource:@"UIWebViewSearch" ofType:@"js"];
    NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    [self stringByEvaluatingJavaScriptFromString:jsCode];

    NSString *startSearch = [NSString stringWithFormat:@"uiWebview_HighlightAllOccurencesOfString('%@')",str];
    [self stringByEvaluatingJavaScriptFromString:startSearch];

    NSString *result = [self stringByEvaluatingJavaScriptFromString:@"uiWebview_SearchResultCount"];
    return [result integerValue];




- (void)removeAllHighlights

    [self stringByEvaluatingJavaScriptFromString:@"uiWebview_RemoveAllHighlights()"];


- (void)GoNext

    [self stringByEvaluatingJavaScriptFromString:@"goNext()"];

@end

问题或疑问是我声明此功能正确吗?

- (void)GoNext
    
        [self stringByEvaluatingJavaScriptFromString:@"goNext()"];
     

因为什么都没有发生,程序不会转到下一个突出显示的单词。非常感谢您的帮助!!!!

【问题讨论】:

查看这个提到添加 javascript 函数的答案:***.com/a/10289485/2274694 【参考方案1】:
    //   i used that code For Next and Previous Functionality And it works fine
     //  function

 uiWebview_HighlightAllOccurencesOfNextStringForElement(element,keyword) 

        if (element) 
            if (element.nodeType == 3)         // Text node
                while (true) 
                    //if (counter < 1) 
                    var value = element.nodeValue;  // Search for keyword in text node
                    var idx = value.toLowerCase().indexOf(keyword);

                    if (idx < 0) break;             // not found, abort

                    var span = document.createElement("span");
                    var text = document.createTextNode(value.substr(idx,keyword.length));
                    span.appendChild(text);
                    span.setAttribute("class","MyAppHighlight");
                    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;
                    span.scrollIntoView();
                    span.style.backgroundColor = "yellow";

                    span.style.color = "black";
                    a.push(span);

                    uiWebview_SearchResultCount++;



                
             else if (element.nodeType == 1)  // Element node
                if (element.style.display != "none" && element.nodeName.toLowerCase() != 'select') 
                    for (var i=element.childNodes.length-1; i>=0; i--) 
                        uiWebview_HighlightAllOccurencesOfNextStringForElement(element.childNodes[i],keyword);
                    
                
            
        
    

    // the main entry point to start the search
    function uiWebview_HighlightAllOccurencesOfNextString(keyword) 
    `enter code here`
        uiWebview_RemoveAllHighlights();
        uiWebview_HighlightAllOccurencesOfNextStringForElement(document.body, keyword.toLowerCase());

    






//And than In Your ViewContrlller Called this function on Next And Previous button method


    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
    
        [self highlightAllOccurencesOfNextString:searchbar.text];
    

    - (NSInteger)highlightAllOccurencesOfNextString:(NSString*)str
    
        NSString *filePath = [[NSBundle mainBundle]pathForResource:@"UIWebViewSearch" ofType:@"js"];
        NSData *fileData = [NSData dataWithContentsOfFile:filePath];
        NSString *jsString = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
        [htmlWebView stringByEvaluatingJavaScriptFromString:jsString];

        NSString *startSearch = [NSString stringWithFormat:@"uiWebview_HighlightAllOccurencesOfNextString('%@')",str];
        [htmlWebView stringByEvaluatingJavaScriptFromString:startSearch];
        NSString *result = [htmlWebView stringByEvaluatingJavaScriptFromString:@"a.length"];
        currentPosition = [result intValue] - 1;
        return [result integerValue];

    

    -(void)nextMethod
    
        currentPosition -= 1;
        NSString *nextScrollPosition =  [NSString stringWithFormat:@"a[%d].scrollIntoView()", currentPosition];
        [htmlWebView stringByEvaluatingJavaScriptFromString:nextScrollPosition];
    

    -(void)previousMethod
    
        currentPosition += 1;
        NSString *previousScrollPosition =  [NSString stringWithFormat:@"a[%d].scrollIntoView()", currentPosition];
        [htmlWebView stringByEvaluatingJavaScriptFromString:previousScrollPosition];
    

【讨论】:

【参考方案2】:

您可以使用函数“highlightAllOccurencesOfNextString”,而不是在您的 Java 脚本文件中使用函数 goNext 和 goPrev,此函数满足您的要求,而不是按钮方法,即 goNext 和其他方法,您可以使用我的答案中的下一个和上一个方法。

【讨论】:

以上是关于UIWebView 下一次出现来自搜索问题的单词的主要内容,如果未能解决你的问题,请参考以下文章

可选项元素?

iOS下JS与OC互相调用--UIWebView 拦截URL

从输入字段中查找部分单词匹配,我还发现来自 HTML 代码的结果。为啥?

如何在搜索栏中保留搜索到的单词?

在 uı 中显示使用 C# 的游戏引擎的搜索百分比 [关闭]

reCAPTCHA 在 iOS 10 UIWebView 中不起作用