【问题标题】:How to hide image with src in wkwebview when loading html in ios?在ios中加载html时如何在wkwebview中隐藏带有src的图像?
【发布时间】:2015-07-18 07:19:25
【问题描述】:

我正在使用 WKWebView 加载 html 字符串,html 字符串的某些末尾有一些丑陋的图片链接,我想隐藏它们。

css用来隐藏图片,但不起作用。

.article img[src* = "/smilies/"],
.article img[src* = ".feedburner.com/~ff/"],
.article img[src* = ".feedburner.com/~r/"],
.article img[src* = ".feedblitz.com/"]
{
    display: none;
}

我想隐藏的带有 feedburner src 的示例 html 字符串:

<div>
<a href="http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Venturebeat?d=yIl2AUoC8zA" border="0"></a> <a href="http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Venturebeat?d=qj6IDK7rITs" border="0"></a> <a href="http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:V_sGLiPBpWU" border="0"></a> <a href="http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/Venturebeat?d=I9og5sOYxJI" border="0"></a> <a href="http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:D7DqB2pKExk" border="0"></a>
</div>

【问题讨论】:

    标签: html ios css webkit wkwebview


    【解决方案1】:

    实现此目的的一种快速而肮脏的方法是使用正则表达式。请注意,这对于长 HTML 文件确实不理想,因为它不如真正的 HTML 解析器高效。

    // The HTML you posted
    NSString *HTML = @"<div>\n\t<a href=\"http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:yIl2AUoC8zA\"><img src=\"http://feeds.feedburner.com/~ff/Venturebeat?d=yIl2AUoC8zA\" border=\"0\"></a> <a href=\"http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:qj6IDK7rITs\"><img src=\"http://feeds.feedburner.com/~ff/Venturebeat?d=qj6IDK7rITs\" border=\"0\"></a> <a href=\"http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:V_sGLiPBpWU\"><img src=\"http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:V_sGLiPBpWU\" border=\"0\"></a> <a href=\"http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:I9og5sOYxJI\"><img src=\"http://feeds.feedburner.com/~ff/Venturebeat?d=I9og5sOYxJI\" border=\"0\"></a> <a href=\"http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:D7DqB2pKExk\"><img src=\"http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:D7DqB2pKExk\" border=\"0\"></a>\n</div>";
    
    // A string containing source of the images that you want to delete
    NSString *source = @"http://feeds.feedburner.com/~ff/";
    
    // Builds a pattern that matches the tags of the images you want to delete
    NSString *pattern = [NSString stringWithFormat:@"<img src=\"%@.+?>", source];
    
    // The actual delete operation
    NSString *cleanHTML = [HTML stringByReplacingOccurrencesOfString:pattern
                                                          withString:@""
                                                             options:NSRegularExpressionSearch
                                                               range:NSMakeRange(0, HTML.length)];
    
    // Do what you want with the cleaned HTML (display it, ...)
    NSLog(@"%@", cleanHTML);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-15
      • 2020-05-18
      • 1970-01-01
      • 2013-09-15
      相关资源
      最近更新 更多