UIWebView中OC 与 JS 交互的语法总结

UIWebView中OC 与 JS 交互的语法总结, 需要注意的是有些JS的执行需要在网页加载完毕之后才可以调用。 即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之间的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.location.href"];

//获取到了返回的url
NSString *backUrl = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(‘back').href"];

// 打开另一个新页面
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.location.href(\"%@\")",@"http://www.baidu.com"]];

// 替换当前页面
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.location.replace(\"%@\")",@"http://www.baidu.com"]];

注: replace函数在设置URL方面与location的href属性或assign函数完全一样,但是它会删除history对象的地址列表中的URL,从而使go或back等函数无法导航。 

// 禁用用户选择
[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
}



其他的一些交互


document:属性
document.title //设置文档标题等价于HTML的

document.bgColor //设置页面背景色

document.fgColor //设置前景色(文本颜色)

document.linkColor //未点击过的链接颜色

document.alinkColor //激活链接(焦点在此链接上)的颜色

document.vlinkColor //已点击过的链接颜色

document.URL //设置URL属性从而在同一窗口打开另一网页

document.fileCreatedDate //文件建立日期,只读属性

document.fileModifiedDate //文件修改日期,只读属性

document.fileSize //文件大小,只读属性

document.cookie //设置和读出cookie

document.charset //设置字符集 简体中文:gb2312

document:方法

document.write() //动态向页面写入内容

document_createElement_x_x_x(Tag) //创建一个html标签对象

document.getElementByIdx_xx_x_x(ID) //获得指定ID值的对象

document.getElementsByName(Name) //获得指定Name值的对象



body:子对象

document.body //指定文档主体的开始和结束等价于

document.body.bgColor //设置或获取对象后面的背景颜色
document.body.link //未点击过的链接颜色
document.body.alink //激活链接(焦点在此链接上)的颜色
document.body.vlink //已点击过的链接颜色
document.body.text //文本色
document.body.innerText //设置…之间的文本
document.body.innerHTML //设置…之间的HTML代码
document.body.topMargin //页面上边距
document.body.leftMargin //页面左边距
document.body.rightMargin //页面右边距
document.body.bottomMargin //页面下边距
document.body.background //背景图片
document.body.a(oTag) //动态生成一个HTML对象

location:子对象

document.location.hash // #号后的部分

document.location.host // 域名+端口号

document.location.hostname // 域名

document.location.href // 完整URL

document.location.pathname // 目录部分

document.location.port // 端口号

document.location.protocol // 网络协议(http:)

document.location.search // ?号后的部分
常用对象事件:

documeny.location.reload() //刷新网页

document.location.reload(URL) //打开新的网页

document.location.assign(URL) //打开新的网页

document.location.replace(URL) //打开新的网页

selection-选区子对象

document.selection

document.documentElement.innerHTML  // 获取HTML所有内容
document.head.innerHTML  // 打印head的内容
document.body.innerHTML // 打印body的内容 

设置页面的初始缩放比例:
NSString *meta = [NSString stringWithFormat:@"document.getElementsByName('viewport')[0].content = 'minimum-scale=0.5; maximum-scale=5; initial-scale=0.5;'"];
    [webView stringByEvaluatingJavaScriptFromString:meta];
这里只有当[webView setScalesPageToFit:YES];时才能支持用户手势缩放view。但是,我在iOS 6.0及7.1上发现minimum-scale会因设置了fit而保持在最小fit状态,无法缩放到0.5,但初始化时是0.5。

缩放页面内容的方法:
在[webView setScalesPageToFit:NO];的前提下,执行:
[webView stringByEvaluatingJavaScriptFromString:@"document.body.style.zoom = 5.0;"];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值