• js与webview 常用交互代码


    常用js交互

    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 . 

     

  • 相关阅读:
    linux之查找文件,目录命令
    linux文件操作常用命令
    linux打包解包压缩解压命令
    linux目录操作常用命令
    php读取文件行数方法
    前端入门之——javascript day8 DOM对象(DHTML)
    前端入门之——javascript day8
    前端入门之——css day5 作业。编写一个简单的网页
    前端入门之——css day4
    前端入门之——css day3
  • 原文地址:https://www.cnblogs.com/iOS-mt/p/4319827.html
Copyright © 2020-2023  润新知