先简单地说一下思路吧
- 需求:在App中嵌入一个webView,已经有了网址,但是需要去掉这个网页中的一些内容
- 解决方案:取到webView要加载的网址,对这个网址对应的网页信息进行修改
- 简单描述:
- 遵守webView的协议
<UIWebViewDelegate>
- 设置代理
- 实现相应代理方法,对网页信息中要删除的内容对应到节点,并删除
- 删除的部分处理:获取到相应的节点并执行删除操作的语句写法有三种方式:见http://www.w3school.com.cn/js/js_htmldom.asp
- 遵守webView的协议
1 #import "JSHanledWebViewController.h" 2 3 //1.遵守协议 4 @interface JSHanledWebViewController ()<UIWebViewDelegate> 5 @property (weak, nonatomic) IBOutlet UIWebView *webView; 6 7 @end 8 9 @implementation JSHanledWebViewController 10 11 - (void)viewDidLoad { 12 [super viewDidLoad]; 13 14 //2.设置代理 15 self.webView.delegate = self; 16 [self loadWebView]; 17 18 } 19 20 #pragma mark - 加载webView 21 - (void)loadWebView{ 22 23 [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.dianping.com/tuan/deal/66666"]]]; 24 //关闭webView的弹簧效果 25 self.webView.scrollView.bounces = NO; 26 //隐藏滚动条 27 self.webView.scrollView.showsVerticalScrollIndicator = NO; 28 29 30 } 31 32 //3.实现相应代理方法 33 #pragma mark - Sent after a web view finishes loading a frame. 34 - (void)webViewDidFinishLoad:(UIWebView *)webView{ 35 36 NSMutableString *jsStrM = [NSMutableString string]; 37 38 //参考w3c: http://www.w3school.com.cn/js/js_htmldom.asp 39 //注意取出来的是个数组,注意指定下标 40 //取到顶部的导航栏 41 [jsStrM appendString:@"var header = document.getElementsByTagName('header')[0];"]; 42 [jsStrM appendString:@"header.parentNode.removeChild(header);"]; 43 44 //取到网页轮播器下的 立即购买 45 [jsStrM appendString:@"var costBox = document.getElementsByClassName('cost-box')[0];"]; 46 [jsStrM appendString:@"costBox.parentNode.removeChild(costBox);"]; 47 48 //取到灰色的立即购买 49 [jsStrM appendString:@"var buy = document.getElementsByClassName('buy-now btn-gray')[0];"]; 50 [jsStrM appendString:@"buy.parentNode.removeChild(buy);"]; 51 52 //取到底部的首页 点评团... 53 [jsStrM appendString:@"var footer = document.getElementsByClassName('footer')[0];"]; 54 [jsStrM appendString:@"footer.parentNode.removeChild(footer);"]; 55 56 //取到黄色的立即购买按钮 57 [jsStrM appendString:@"var footerBtn = document.getElementsByClassName('footer-btn-fix')[0];"]; 58 [jsStrM appendString:@"footerBtn.parentNode.removeChild(footerBtn);"]; 59 60 //The stringByEvaluatingJavaScriptFromString: method waits synchronously for JavaScript evaluation to complete. 61 [webView stringByEvaluatingJavaScriptFromString:jsStrM]; 62 63 64 } 65 66 @end
- 最终的效果如下所示
如有问题,敬请指正!