下面的代码用于修改html文本中的img标记,修改后的html适用于lazyload方式的图片加载:

        protected string LazyPicProcess(string content) {
            Regex re = new Regex("<\\s*img[\\s\\S]*?(?<src>src[\\s]*=[\\s]*[\"|\'](?<pic>[\\s\\S]*?)[\"|\'])[\\s\\S]*?>", RegexOptions.                  IgnoreCase);
            string str = re.Replace(content, new MatchEvaluator(ImgSrcReplace));
            return str;
        }
        public static string ImgSrcReplace(Match match) {
            string newtext = string.Empty;
            if (match.Groups.Count > 2) {
                string img = match.Groups[0].Value;
                string src = match.Groups["src"].Value;
                string pic = match.Groups["pic"].Value;
                newtext = img.Replace(src, "src=\"images/grey.gif\" data-img=\"" + pic + "\" isload=\"false\"");
                return newtext;
            }
            else {
                return match.Groups[0].Value;
            }
}

 

 

 

关于延迟加载图片参考博文:

JS图片延迟加载分析及简单的demo

 

相关文章:

  • 2022-12-23
  • 2021-11-10
  • 2022-12-23
  • 2021-10-08
  • 2021-11-25
  • 2021-10-14
  • 2021-06-07
  • 2022-12-23
猜你喜欢
  • 2022-01-05
  • 2022-12-23
  • 2022-02-06
  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2021-05-28
相关资源
相似解决方案