美文网首页
H5 ——删除网页内容

H5 ——删除网页内容

作者: by小杰 | 来源:发表于2016-09-29 17:11 被阅读225次
#import "ViewController.h"

@interface ViewController ()<UIWebViewDelegate>

@end

@implementation ViewController

//每次加载网页请求都会走该方法
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    //NSString *js = @"document.getElementById('btn')";
    
    //获取body标签中的所有标签内容
    //NSString *js = @"document.getElementsByTagName('body')[0].innerHTML";
    
    //获取body标签, 然后用父标签删除子标签,删除body中的img标签
    NSString *js = @"var body = document.getElementsByTagName('body')[0].innerHTML; body.removeChild('img');";
    
    NSString *content = [webView stringByEvaluatingJavaScriptFromString:js];
    NSLog(@"%@", content);
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //服务器文件URL地址
    //http://172.21.62.99/train/index4.html
    
    UIWebView *web = [[UIWebView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:web];
    web.delegate =self;
    
    //加载webView(请求index4.html文件)
    [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://172.21.62.99/train/index4.html"]]];
     
}```

相关文章

网友评论

      本文标题:H5 ——删除网页内容

      本文链接:https://www.haomeiwen.com/subject/tmzkyttx.html