IOS網絡第七天WebView-01WebView和網頁的交互1

********html

#import "HMViewController.h"

@interface HMViewController () <UIWebViewDelegate>

@end

@implementation HMViewController

/**
 test.html存在於服務器,裏面的html和js代碼,咱們是沒法修改的

 若是test.html顯示在手機端,把那個ul去掉
 
 */

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 1.webView
    UIWebView *webView = [[UIWebView alloc] init];
    webView.frame = self.view.bounds;
    webView.delegate = self;
    [self.view addSubview:webView];
    
    webView.scalesPageToFit = YES;
    
    // 2.加載網頁
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/MJServer/resources/htmls/test.html"]];
    [webView loadRequest:request];
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    // 利用webView執行js代碼,操做網頁中的節點
    NSString *js1 = @"var ul = document.getElementsByTagName('ul')[0];";
    NSString *js2 = @"ul.parentNode.removeChild(ul);";
    
    [webView stringByEvaluatingJavaScriptFromString:js1];
    [webView stringByEvaluatingJavaScriptFromString:js2];
}

@end
相關文章
相關標籤/搜索