美文网首页
解决RichText组件图片宽度展示异常的问题

解决RichText组件图片宽度展示异常的问题

作者: zackzheng | 来源:发表于2020-06-07 18:31 被阅读0次

本文始发于我的博文解决Taro下RichText遇到的图片宽度问题,现转发至此。

使用TaroRichText组件,遇到展示图片的宽度超出屏幕的问题,记录下解决办法。可以根据实际需要,对以下方法进行修改。

  • 只有图片标签,并且图片标签没有设置style和宽度/高度
function formatRichText(html){
  let newContent= html.replace(/\<img/gi, '<img style="width:100%;height:auto"');
  return newContent;
}
  • 包含多种标签
/**
 * 处理富文本里的图片宽度自适应
 * 1.去掉img标签里的style、width、height属性
 * 2.img标签添加style属性:max-width:100%;height:auto
 * 3.修改所有style里的width属性为max-width:100%
 * 4.去掉&lt;br/&gt;标签
 * @param html
 * @returns {void|string|*}
 */
function formatRichText(html){
  let newContent= html.replace(/&lt;img[^&gt;]*&gt;/gi,function(match,capture){
    match = match.replace(/style=&quot;[^&quot;]+&quot;/gi, '').replace(/style='[^']+'/gi, '');
    match = match.replace(/width=&quot;[^&quot;]+&quot;/gi, '').replace(/width='[^']+'/gi, '');
    match = match.replace(/height=&quot;[^&quot;]+&quot;/gi, '').replace(/height='[^']+'/gi, '');
    return match;
  });
  newContent = newContent.replace(/style=&quot;[^&quot;]+&quot;/gi,function(match,capture){
    match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
    return match;
  });
  newContent = newContent.replace(/&lt;br[^&gt;]*\/&gt;/gi, '');
  newContent = newContent.replace(/\&lt;img/gi, '&lt;img style=&quot;max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;&quot;');
  return newContent;
}

-END-
欢迎到我的博客交流:https://zackzheng.info

相关文章

网友评论

      本文标题:解决RichText组件图片宽度展示异常的问题

      本文链接:https://www.haomeiwen.com/subject/ncuvzhtx.html