javascript - 转到单词突出显示的位置

标签 javascript ios uiwebview

当我通过 UIWebview 在 html 文件中搜索时,我的应用程序会注册突出显示的单词。我遇到的问题是该程序只是突出显示选定的单词。由于我有几个页面,如何使程序自动转到突出显示单词的位置,而不是我四处滚动,搜索突出显示的单词。到目前为止,这是我的代码。谢谢

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

                uiWebview_SearchResultCount++;    // update the counter

                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;

            }
        } 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);
                }
            }
        }
    }
}

最佳答案

//我将该代码用于下一个和上一个功能并且它工作正常 //函数

uiWebview_HighlightAllOccurencesOfNextStringForElement(元素,关键字){

    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());

}

//并且比在您的 ViewContrlller 中的“下一个”和“上一个”按钮方法上调用此函数

- (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];
}

关于javascript - 转到单词突出显示的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28438309/

相关文章:

ios - 如何在XMPP中获取选定用户的聊天记录

ios - 如何从 UIWebView :shouldStartLoadWithRequest:? 中取消 NSURLRequest

objective-c - 在 webview 上添加事件指示器

javascript - 如何在 FabricJS 上复制/粘贴后保持 IText 可编辑?

javascript - 如何从字符串的特定部分对列表进行数字排序

objective-c - 在 UITableView 中实现图片异步加载的最佳方法

ios - 在 UITableViewCell 中插入 UIWebView 以播放动画 GIF

javascript - 如何从 json 返回特定对象(不区分大小写)?

javascript - 如何使用 vanilla JS 有条件地阻止同一元素的事件处理程序?

ios - 更新 CoreData 中的项目不会刷新 tableView