iOS UIWebView与JavaScript交互

- (void)webViewDidFinishLoad:(UIWebView *)webView{

    //获得UIWebViewURL地址

    NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];

    NSLog(@"currentURL==%@",currentURL);

    //获得UIWebView的标题

    NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];

    NSLog(@"theTitle==%@",theTitle);

    //通过name(获得/设置)页面元素的value

    NSString *js_email_ByName = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('email')[0].value='hello';"];

    NSLog(@"js_email_ByName==%@",js_email_ByName);

    NSString *js_password_ByName = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('pwd')[0].value='hello';"];

    NSLog(@"js_password_ByName==%@",js_password_ByName);

    NSString *js_phone_ByName = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('tel')[0].value='hello';"];

    NSLog(@"js_phone_ByName==%@",js_phone_ByName);

    //通过id(获得/设置)页面元素的value

//    NSString *js_email_ById = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementByIdx_x_x('_iphone_email').value='asdfasdf';"];

//    NSLog(@"js_email_ById==%@",js_email_ById);

//    NSString *js_password_ById = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementByIdx_x_x('_iphone_pwd').value='asdfasdf';"];

//    NSLog(@"js_password_ById==%@",js_password_ById);

//    NSString *js_phone_ById = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementByIdx_x_x('_iphone_phone').value='asdfasdf';"];

//    NSLog(@"js_phone_ById==%@",js_phone_ById);   

   

    //提交表单

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

//    NSLog(@"js_forms==%@",js_forms);

   

    //获得bodybody之间的HTML

    NSString *allHTML = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];

    NSLog(@"allHTML: %@", allHTML);

    //使UIWebView的输入框获得焦点(但是无法,弹出iphone键盘)

[webView stringByEvaluatingJavaScriptFromString:@"document.querySelector('#saySome').focus()"];

[webView stringByEvaluatingJavaScriptFromString:@"document.getElementByIdx_x("saySome").scrollIntoView("true")"];

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值