如何在UIWebView中编辑HTML?

Dec*_*ple 3 uiwebview ios swift swift2

我有一个加载HTML的UIWebView.如何访问这些HTML元素并进行更改?

我想做的事情如下: webView.getHTMLElement(id: main-title).value = "New title"

Gil*_* AB 6

如果要在表单中编辑它,可以使用以下方法:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
   NSString *evaluate = [NSString stringWithFormat:@"document.form1.main-title.value='%@';", @"New title"];
   [webView stringByEvaluatingJavaScriptFromString:evaluate];
}
Run Code Online (Sandbox Code Playgroud)

或者如果不是一种形式,也许这(未经测试):

- (void)webViewDidFinishLoad:(UIWebView *)webView {
   NSString *evaluate = [NSString stringWithFormat:@"document.getElementById('main-title').value='%@';", @"New title"];
   [webView stringByEvaluatingJavaScriptFromString:evaluate];
}
Run Code Online (Sandbox Code Playgroud)

注意!我认为这是一个你想要改变的可编辑字段.否则你正在谈论解析,这个概念的工作原理如下:

    static BOOL firstLoad = YES;

    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        if (firstLoad) {
            firstLoad = NO;
            NSString *html = [_webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML"];
            //Edit html here, by parsing or similar.
            [webView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];
        }
    }
Run Code Online (Sandbox Code Playgroud)

您可以在此处阅读有关解析的更多信息:Objective-C html解析器