webview使用技巧汇总

时间:2021-01-11 08:51:19

1、webview去除原网址的广告或者标题

js语句

document.documentElement.getElementsByClassName('这里写你要消除的空间的class里面的字符串')[0].style.display = 'none'

具体用法:

- (void)webViewDidFinishLoad:(UIWebView *)webView{

[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.getElementsByClassName('adpic')[0].style.display = 'none'"];

}

2. 获得UIWebView的标题

- (void)webViewDidFinishLoad:(UIWebView *)webView{

NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];

}

3. 获取页面的URL

- (void)webViewDidFinishLoad:(UIWebView *)webView{

NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];

}

4. 修改页面元素的值

- (void)webViewDidFinishLoad:(UIWebView *)webView{

NSString *js_result = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('q')[0].value='朱祁林';"];

}

5. 表单提交

- (void)webViewDidFinishLoad:(UIWebView *)webView{

NSString *js_result2 = [webView stringByEvaluatingJavaScriptFromString:@"document.forms[0].submit(); "]
}