8.UIWebView与JS交互的语法总结

UIWebView 与 JS 交互的语法总结,写在UIWebView的delegate中,写在 - (void)webViweDidFinishLoad:(UIWebView *)webView方法中。

// 获得UIWebView的URL地址
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
// 获得UIWebView 的标题
NSString *theTitle = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
// 通过name(获得/设置)页面元素的value值
NSString *js_email_ByName = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('email')[0].value = 'hello';"];
NSString *js_password_ByName  = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('pwd')[0].value = 'hello';"];
NSString *js_phone_ByName = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('tel')[0].value = 'hello';"];
// 通过id(获得/设置)页面元素的value值
// NSString *js_email_ById  = [webView stringByEvaluetingJavaScriptFromString:@"document.getElementByIdx_x_x('_iphone_email').value='asdfasdf';"];
// NSString *js_password_ById = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementByIdx_x_x('_iphone_pwd').value='asdfasdf';"];
// NSString *js_phone_ById = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByIdx_x_x('_iphone_phone').value='asdfasdf';"];

// 提交表单
// NSString *js_forms  = [webView stringByEvaluatingJavaScriptFromString:@"document.forms[0].submit()"];

// 获得body与body之间的HTML
NSString *allHTML = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];

// 使UIWebView的输入框获得焦点(但是无法弹出iphone键盘)
[webView stringByEvaluatingJavascriptFromString:@"document.querySelector('#saySome').focus()"];
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementByIdx_x("saySome").scrollIntoView("true")"];
//获取到了返回的url
NSString *backUrl = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(‘back').href"];

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.location.replace(%@)",_backUrl]];

// 禁用用户选择
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
// 禁用长按弹出框
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
// 隐藏titleBar
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('titlebar').style.display = 'none';"];

//获取 webview 中内容的高度,做自适应
NSString *height_str = [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"];
int height = [height_str intValue];
//注:改变自适应高度时,创建 UIWebView 时,应将其frame 的高度设置为尽可能小但不可为0,可设为1.这样在 webViewDidFinishLoad:方法中才能变为可适应高度




//字体大小
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= ‘330%'"];
//字体颜色
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'gray'"];
//页面背景色
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#2E2E2E'"];

//背景色透明可以用正常OC代码控制
[webView setBackgroundColor:[UIColor clearColor]];
[webView setOpaque:NO];


NSString *result = [webView stringByEvaluatingJavaScriptFromString: @"document"];

result = [webView stringByEvaluatingJavaScriptFromString: @"document.body"];

result = [webView stringByEvaluatingJavaScriptFromString: @"document.body.getElementsByClassName"];

result = [webView stringByEvaluatingJavaScriptFromString: @"document.body.getElementsByClassName('box')[0]"];

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //webview 自适应高度
    CGRect frame = webView.frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    webView.frame = frame;
    //tableView reloadData
}

你可能感兴趣的:(8.UIWebView与JS交互的语法总结)