从后端接口传过来的数据里的html代码能正常显示在小程序页面上吗
从后端接口传过来的数据里的html代码能正常显示在小程序页面上吗
从后端接口传过来的数据里的html代码能正常显示在小程序页面上吗
说下我的解决思路
服务器后台获取到html之后
根据p标签或者div标签 分割后循序 保存到数组里面
有区别 是文字还是图片、方便在 小程序里 绑定
获取到文章之后。过滤html标签
贴上 c# 语言实现的代码
public static List<CntList> getCntByList(string content) { List<CntList> listCnt = new List<CntList>(); //文章 if (!string.IsNullOrEmpty(content)) { HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(content); var p_cnt = doc.DocumentNode.SelectNodes("//p"); foreach (var pItem in p_cnt) { var node = HtmlNode.CreateNode(pItem.OuterHtml); if (node.InnerHtml.Contains("src")) { if (node.SelectSingleNode("//img") != null) { string _href = node.SelectSingleNode("//img").Attributes["src"].Value; listCnt.Add(new CntList { type = "image", content = _href }); } } else { listCnt.Add(new CntList { type = "content", content = X.Component.Tools.StringHelper.NoHTML(node.InnerText) }); } } } return listCnt; } |
效果: