V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
lfflyers
V2EX  ›  iDev

iOS 调用 JS 无效,求大神指教!!!

  •  
  •   lfflyers · 2015-11-20 11:23:00 +08:00 · 5342 次点击
    这是一个创建于 3052 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这是 webview 的实现:
    _webView = [[UIWebView alloc]init];
    _webView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT + 45);
    _webView.scalesPageToFit = YES;
    _webView.delegate = self;
    _webView.userInteractionEnabled = YES;
    [self.view addSubview:_webView];

    NSURL *url = [[NSURL alloc]initWithString:@"http://m.weather.com.cn/mweather/101280601.shtml"];
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
    [_webView loadRequest:request];
    

    这是 webview 的代理方法
    - (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSMutableString *js1 = [NSMutableString string];

    // 0.删除顶部的导航条
    [js1 appendString:@"var header = document.getElementsByClassName('head')[0];"];
    [js1 appendString:@"header.parentNode.removeChild(header);"];
    
    // apostertop
    [js1 appendString:@"var apostertop = document.getElementsByClassName('apostertop')[0];"];
    [js1 appendString:@"apostertop.parentNode.removeChild(apostertop);"];
    
    [js1 appendString:@"var news = document.getElementsByClassName('news');"];
    [js1 appendString:@"news.parentNode.removeChild(news);"];
    
    [js1 appendString:@"var ad = document.getElementsByClassName('ad');"];
    [js1 appendString:@"ad.parentNode.removeChild(ad);"];
    
    [js1 appendString:@"var life = document.getElementsByClassName('life');"];
    [js1 appendString:@"life.parentNode.removeChild(life);"];
    
    //zs
    [js1 appendString:@"var zs = document.getElementsByClassName('zs');"];
    [js1 appendString:@"zs.parentNode.removeChild(zs);"];
    
    //footer
    [js1 appendString:@"var footer = document.getElementsByClassName('footer');"];
    [js1 appendString:@"footer.parentNode.removeChild(footer);"];
    
    [webView stringByEvaluatingJavaScriptFromString:js1];
    

    }

    问题来了,执行后,显示上没有任何变化,求大神指教!

    11 条回复    2015-11-20 13:03:40 +08:00
    doublleft
        1
    doublleft  
       2015-11-20 11:25:58 +08:00
    是不是取出的 nodelist 没加下标
    lfflyers
        2
    lfflyers  
    OP
       2015-11-20 11:32:14 +08:00
    @doublleft 就是那个[0]吗,我加了也没效果,不加也没效果
    lfflyers
        3
    lfflyers  
    OP
       2015-11-20 11:36:14 +08:00
    先自顶一下~~
    black
        4
    black  
       2015-11-20 11:39:57 +08:00
    removeChild 参数一会儿传 node ,一会儿传 nodes 是要闹哪样...
    finian
        5
    finian  
       2015-11-20 11:46:09 +08:00
    题外话, JS 就不能写在一个字符串里吗? 现在这分散的语句看起来和维护起来都很蛋疼
    finian
        6
    finian  
       2015-11-20 11:47:16 +08:00
    可以用 Safari 调试一下,执行你的 JS ,看哪些生效哪些不生效
    doublleft
        7
    doublleft  
       2015-11-20 12:13:55 +08:00
    stringByEvaluatingJavaScriptFromString 可以先引入 js 文件,然后再执行的,方便定位一些
    lfflyers
        8
    lfflyers  
    OP
       2015-11-20 12:24:31 +08:00
    @black 我就想试试看看那个生效~~,怎么删除 nodes?
    lfflyers
        9
    lfflyers  
    OP
       2015-11-20 12:31:00 +08:00
    @finian 我是想加载一个天气的 webview ,然后把不相干的东西全部删掉
    lfflyers
        10
    lfflyers  
    OP
       2015-11-20 12:34:17 +08:00
    @doublleft 没弄过,不知道怎么搞
    lfflyers
        11
    lfflyers  
    OP
       2015-11-20 13:03:40 +08:00
    问题,蛋疼,第一条的 class 写错后,后面的都不执行了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2786 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:04 · PVG 23:04 · LAX 08:04 · JFK 11:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.