zoukankan      html  css  js  c++  java
  • UIWebview 截获html并修改内容。


    需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果; 

    方法:通过js截获UIWebView中的html,然后修改html标签内容; 
    实例代码: 
    服务器端html

    Java代码  收藏代码
    1. <html><head>  
    2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
    3. <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">   
    4. <title>网曝四川省一考场时钟慢半小时 老师称这就是命</title></head<body>网曝四川省一考场时钟慢半小时 老师称这就是命</body></html>  


    这样显示的结果网页的最小宽度会是device-width;但有时候不需要这个宽度,就需要修改width=device-width为width=myWidth; 
    客户端代码

    Java代码  收藏代码
    1. - (void)webViewDidFinishLoad:(UIWebView *)webView  
    2. {     
    3.     //修改服务器页面的meta的值  
    4.     NSString *meta = [NSString stringWithFormat:@"document.getElementsByName("viewport")[0].content = "width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"", webView.frame.size.width];  
    5.     [webView stringByEvaluatingJavaScriptFromString:meta];  
    6. }  


    这样问题就可以解决了 

    新增代码: 

    Java代码  收藏代码
    1. //给网页增加utf-8编码  
    2.  [webView stringByEvaluatingJavaScriptFromString:  
    3.  @"var tagHead =document.documentElement.firstChild;"  
    4.   "var tagMeta = document.createElement("meta");"   
    5.   "tagMeta.setAttribute("http-equiv", "Content-Type");"   
    6.   "tagMeta.setAttribute("content", "text/html; charset=utf-8");"   
    7.   "var tagHeadAdd = tagHead.appendChild(tagMeta);"];  
    Java代码  收藏代码
    1. //给网页增加css样式  
    2.     [webView stringByEvaluatingJavaScriptFromString:  
    3.      @"var tagHead =document.documentElement.firstChild;"  
    4.      "var tagStyle = document.createElement("style");"   
    5.      "tagStyle.setAttribute("type", "text/css");"   
    6.      "tagStyle.appendChild(document.createTextNode("BODY{padding: 20pt 15pt}"));"  
    7.      "var tagHeadAdd = tagHead.appendChild(tagStyle);"];  


    Java代码  收藏代码
    1. //拦截网页图片  并修改图片大小        
    2. [webView stringByEvaluatingJavaScriptFromString:  
    3.  @"var script = document.createElement('script');"   
    4.  "script.type = 'text/javascript';"   
    5.  "script.text = "function ResizeImages() { "   
    6.      "var myimg,oldwidth;"  
    7.      "var maxwidth=380;" //缩放系数   
    8.      "for(i=0;i <document.images.length;i++){"   
    9.          "myimg = document.images[i];"  
    10.          "if(myimg.width > maxwidth){"   
    11.              "oldwidth = myimg.width;"   
    12.              "myimg.width = maxwidth;"   
    13.              "myimg.height = myimg.height * (maxwidth/oldwidth);"   
    14.          "}"   
    15.      "}"   
    16.  "}";"   
    17.  "document.getElementsByTagName('head')[0].appendChild(script);"];   
    18.   
    19. [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];  


    其他html属性重载和此方法类似; 
    参考网址: 
    (stringByEvaluatingJavaScriptFromString的使用方法)http://www.uml.org.cn/mobiledev/201108181.asp   
    ( iphone 获取UIWebView内Html方法)http://blog.csdn.net/diyagoanyhacker/article/details/6564897 
    (IOS UIWebView引用外部CSS样式)http://hi.baidu.com/jwq359699768/item/780879e5c98bfb3e4ddcaf22 
    http://blog.csdn.net/xdonx/article/details/6973521

  • 相关阅读:
    Mathematica查看内部定义
    Mathematica绘制曲面交线方法
    Mathematica新特性Inactive, 求解复杂微分方程
    Mathematica修改默认字体
    Mac系统下lipo, ar, nm等工具的使用简介
    centos8 安装php7.2以及php-fpm
    mysql8.0创建用户只能访问某一个数据库
    CentOS 7 yum安装 RabbitMQ
    Linux服务器PHP+MYSQL环境配置优化提升网站运行效率
    PHP 7.1安装xhprof进行性能分析
  • 原文地址:https://www.cnblogs.com/lingzhao/p/3388136.html
Copyright © 2011-2022 走看看