1.利用webView控件加载本地html5或者网络上html5
2.设置控制器为webView的代理,遵守协议
3.实现代理方法webViewDidFinishLoad:
4.在代理方法中进行操作HTML5中的标签(CRUD→增删改查)
1.用类选择器或者id选择器或者标签选择器等选择要进行操作的标签
2.把选择好的标签转换成字符串
3.webView调用stringByEvaluatingJavaScriptFromString方法
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];
}
网友评论