先简单地说一下思路吧
- 需求:在App中嵌入一个webView,已经有了网址,但是需要去掉这个网页中的一些内容
- 解决方案:取到webView要加载的网址,对这个网址对应的网页信息进行修改
- 简单描述:
- 遵守webView的协议
<UIWebViewDelegate>
- 设置代理
- 实现相应代理方法,对网页信息中要删除的内容对应到节点,并删除
- 删除的部分处理:获取到相应的节点并执行删除操作的语句写法有三种方式:见http://www.w3school.com.cn/js/js_htmldom.asp
- 遵守webView的协议
#import "JSHanledWebViewController.h" //1.遵守协议
@interface JSHanledWebViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView; @end @implementation JSHanledWebViewController - (void)viewDidLoad {
[super viewDidLoad]; //2.设置代理
self.webView.delegate = self;
[self loadWebView]; } #pragma mark - 加载webView
- (void)loadWebView{ [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.dianping.com/tuan/deal/66666"]]];
//关闭webView的弹簧效果
self.webView.scrollView.bounces = NO;
//隐藏滚动条
self.webView.scrollView.showsVerticalScrollIndicator = NO; } //3.实现相应代理方法
#pragma mark - Sent after a web view finishes loading a frame.
- (void)webViewDidFinishLoad:(UIWebView *)webView{ NSMutableString *jsStrM = [NSMutableString string]; //参考w3c: http://www.w3school.com.cn/js/js_htmldom.asp
//注意取出来的是个数组,注意指定下标
//取到顶部的导航栏
[jsStrM appendString:@"var header = document.getElementsByTagName('header')[0];"];
[jsStrM appendString:@"header.parentNode.removeChild(header);"]; //取到网页轮播器下的 立即购买
[jsStrM appendString:@"var costBox = document.getElementsByClassName('cost-box')[0];"];
[jsStrM appendString:@"costBox.parentNode.removeChild(costBox);"]; //取到灰色的立即购买
[jsStrM appendString:@"var buy = document.getElementsByClassName('buy-now btn-gray')[0];"];
[jsStrM appendString:@"buy.parentNode.removeChild(buy);"]; //取到底部的首页 点评团...
[jsStrM appendString:@"var footer = document.getElementsByClassName('footer')[0];"];
[jsStrM appendString:@"footer.parentNode.removeChild(footer);"]; //取到黄色的立即购买按钮
[jsStrM appendString:@"var footerBtn = document.getElementsByClassName('footer-btn-fix')[0];"];
[jsStrM appendString:@"footerBtn.parentNode.removeChild(footerBtn);"]; //The stringByEvaluatingJavaScriptFromString: method waits synchronously for JavaScript evaluation to complete.
[webView stringByEvaluatingJavaScriptFromString:jsStrM]; } @end
- 最终的效果如下所示
如有问题,敬请指正!