[转载] js与webview 常用交互代码

来源:点此  http://www.cnblogs.com/iOS-mt/p/4319827.html

 

css常用参数:::

是否允许用户选择元素的内容,选择值包括: 
   auto:用户可以选择元素内的内容 
   none:用户不能选择任何内容 
   text:用户只能选择元素内的文本 

 

常用JS语句:::

1、 //禁用用户选择

    [self.webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];

 

2、//禁用长按弹出框

    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];

 

3、//获得UIWebView的URL地址

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

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

 

4、//获得UIWebView的标题

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

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

 

5、//通过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);

 

6、//通过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);

 

7、//提交表单

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

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

   

8、//获得body与body之间的HTML

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

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

 

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

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

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

 

10、//改变webview尺寸时对应改变web page尺寸(web page需要有对应的处理)

    [webview stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat:@"document.querySelector('meta[name=viewport]').setAttribute('content', 'width=%d;', false); ",(int)webview.frame.size.width]];

   

11、//获取webview显示内容的高度

    CGFloat documentWidth = [[wb stringByEvaluatingJavaScriptFromString:@"document.getElementById('content').offsetWidth"] floatValue];

CGFloat documentHeight = [[wb stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"content\").offsetHeight;"] floatValue];

 

12、//通过id获取内容

    NSString *js = @"document.getElementById('lg').innerHTML";

    NSString *pageSource = [webView stringByEvaluatingJavaScriptFromString:js];

    NSLog(@"pagesource:%@", pageSource);

 

13、//改变字体大小

  [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= ’150%’"];

 

14、//改变webView中图片大小

    [webView stringByEvaluatingJavaScriptFromString:

     @"var script = document.createElement('script');"

     "script.type = 'text/javascript';"

     "script.text = \"function ResizeImages() { "

     "var myimg,oldwidth;"

     "var maxwidth = 300.0;" // UIWebView中显示的图片宽度

     "for(i=0;i <document.images.length;i++){"

     "myimg = document.images[i];"

     "if(myimg.width > maxwidth){"

     "oldwidth = myimg.width;"

     "myimg.width = maxwidth;"

     "}"

     "}"

     "}\";"

     "document.getElementsByTagName('head')[0].appendChild(script);"];

 

15、//删除所有链接

     [webView stringByEvaluatingJavaScriptFromString:@"$(document).ready(function () {$(\"a\").removeAttr(\"href\");})"];

 

补充:::

UIWebview类中有有一个名为scalesPageToFit的BOOL属性,该属性指定当web页面与UIWebView的大小不一致时,是否缩放web页面来使用 UIWebView组件的大小。默认值为NO,即忽略web页面与webview组件的大小关系,以页面的原始大小进行显示,不执行任何缩放。有时为了保证内容出现滚动条,要确保HTML页面的大小与webview组件的大小的一致性,同时设置webview.scrollView.scrollEnabled  = NO . 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值