1. 程式人生 > >iOS修改UIWebView裡面的HTML標籤和元素的值

iOS修改UIWebView裡面的HTML標籤和元素的值

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [MBProgressHUD dismiss];

    // 修改字型顏色
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];

    // 修改所有元素的背景顏色: 用"*"代表所有元素
    NSString *allElementsStr = @"var objs = document.getElementsByTagName('*'
);\n" "for(var i=0; i<objs.length; i++) {\n" "objs[i].style.backgroundColor='#18171A';} \n"; [webView stringByEvaluatingJavaScriptFromString:allElementsStr]; // 修改段落背景顏色 NSString *pStr = @"var objs = document.getElementsByTagName('p');\n" "for(var i=0; i<objs.length; i++) {\n"
"objs[i].style.backgroundColor='#0B1F4F';} \n"; [webView stringByEvaluatingJavaScriptFromString:pStr]; // 修改div背景顏色 NSString *divStr = @"var objs = document.getElementsByTagName('div');\n" "for(var i=0; i<objs.length; i++) {\n" "objs[i].style.backgroundColor='#0B1F4F';} \n"; [webView stringByEvaluatingJavaScriptFromString:divStr]; // 修改span背景顏色 NSString *spanStr
= @"var objs = document.getElementsByTagName('span');\n"
"for(var i=0; i<objs.length; i++) {\n" "objs[i].style.backgroundColor='#0B1F4F';} \n"; [webView stringByEvaluatingJavaScriptFromString:spanStr]; }