iOS开发iOS开发实用技术iOS编程

iOS-OC中加载html5调用html方法和修改HTML5内容

2015-11-11  本文已影响23459人  船长_

1.加载本地HTML5

NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"html"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];

2.加载网络上网页HTML5

NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];

3.在oc中删除HTML5中的某个标签,比如按钮

// 网页视图加载完毕会调用代理的这个方法
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *str = @"document.getElementsByClassName('detail_btns2')[0].remove();";
    [webView stringByEvaluatingJavaScriptFromString:str];
}

4.在oc中更改HTML5内容

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
   NSString *headerStr = @"document.getElementsByTagName('h1')[0].innerText = '测试文字';";
   [webView stringByEvaluatingJavaScriptFromString:headerStr];

   NSString *downLoadStr = @"document.getElementById('xiazaiapp').getElementsByTagName('a')[0].innerText = '下个鸡蛋';";
   [webView stringByEvaluatingJavaScriptFromString:downLoadStr];
}
上一篇 下一篇

猜你喜欢

热点阅读