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
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、简介 近两年随着HTML5的迅速发展与日趋成熟,越来越多的移动开发者选择使用HTML5来进行混合开发,不仅节约...
    RainyGY阅读 1,909评论 1 12
  • 一、简介 近两年随着HTML5的迅速发展与日趋成熟,越来越多的移动开发者选择使用HTML5来进行混合开发,不...
    宝宝teacher阅读 2,372评论 3 15
  • IOS之UIWebView的使用 刚接触IOS开发1年多,现在对于 混合式 移动端开发越来越流行,因为开发成本上、...
    学无止境666阅读 45,914评论 5 53
  • 前言 Web 页面中的 JS 与 iOS Native 如何交互是每个 iOS 猿必须掌握的技能。而说到 Nati...
    幽城88阅读 2,245评论 1 8
  • 上周末,有风,不适宜室外活动,我和啃泥带着宝宝去逛商场,一来让我撒撒欢儿,二来也让婆婆在家休息休息。逛了两个小时以...
    小柔简简阅读 317评论 0 0