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

来源:互联网 发布:texstudio mac 下载 编辑:程序博客网 时间:2024/05/17 22:47

UIWebView 与 JS 交互的语法总结, 写在 UIWebView的 delegate 中,写在-(void)webViewDidFinishLoad:(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 stringByEvaluatingJavaScriptFromString:@"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.getElementByIdx_x_x('_iphone_phone').value='asdfasdf';"];//提交表单//    NSString *js_forms = [webView stringByEvaluatingJavaScriptFromString:@"document.forms[0].submit(); "];//获得body与body之间的HTMLNSString *allHTML = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];//使UIWebView的输入框获得焦点(但是无法,弹出iphone键盘)[webView stringByEvaluatingJavaScriptFromString:@"document.querySelector('#saySome').focus()"];[webView stringByEvaluatingJavaScriptFromString:@"document.getElementByIdx_x("saySome").scrollIntoView("true")"];//获取到了返回的urlNSString *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]"];.

//else method:

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