

解决Taro下RichText遇到的图片宽度问题
source link: https://zackzheng.info/2019/08/27/resolve-image-width-in-richtext/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

解决Taro下RichText遇到的图片宽度问题
使用Taro
的RichText
组件,遇到展示图片的宽度超出屏幕的问题,记录下解决办法。可以根据实际需要,对以下方法进行修改。
- 只有图片标签,并且图片标签没有设置style和宽度/高度
1
2
3
4
function formatRichText(html){
let newContent= html.replace(/\<img/gi, '<img style="width:100%;height:auto"');
return newContent;
}
- 包含多种标签
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* 处理富文本里的图片宽度自适应
* 1.去掉img标签里的style、width、height属性
* 2.img标签添加style属性:max-width:100%;height:auto
* 3.修改所有style里的width属性为max-width:100%
* 4.去掉<br/>标签
* @param html
* @returns {void|string|*}
*/
function formatRichText(html){
let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
return match;
});
newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
return match;
});
newContent = newContent.replace(/<br[^>]*\/>/gi, '');
newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
return newContent;
}
-END-
欢迎到我的博客交流:https://zackzheng.info
Recommend
-
46
关于时尚营销,你最想了解什么类型的品牌?「国外小众品牌」以82.4%的高票位居榜首。那这类品牌如何做好营销? 筹备新栏目「时尚营销」的过程...
-
23
Convert RichText to HTML in PHP API eZ Platform stores Rich Text internally in XML. To convert this XML to HTML markup common in web projects, you can use t...
-
9
Latex中插入.eps图片遇到的问题 (Unknown graphics extension:.eps) ...
-
14
Span的基础性概念分析国际惯例,官网镇楼,这是对Span最好的全局概览。https://developer.android.com/guide/topics/text/spansSpan种类Span通常按照下...
-
12
Jager · 6月16日 · 2017年python · shell · shell脚本 663次已读最近一直在开发自动化运维...
-
3
写这篇文章的想法来源于最近的工作:对Worktile客户端升级重构和解决现有堆积的 bugs,这对于我来说是个难题,因为这是一个几年前的老项目了,功能和逻辑虽然不是很多,但其中存在着一些历史原因,目前负责该项目的同事已...
-
5
最近把这个没人访问的小站从Typecho转回了Wordpress,没啥特别的原因,只是换了主机想折腾折腾。Typecho很优秀,但一直没更新,给人感觉没人在维护了(刚发现好像马上要出新版了)。 自己所学所做和IT行业半毛钱关系没有,属纯外行。为了整个过程顺利省...
-
6
解决taro小程序中引入axios包过大的问题我们在使用taro 和 @freud/http(公司内部项目,基于axios做的二次开发) 的时候,发现构建产物中多了很多没有用的包,导致产物变大了...
-
6
WordPress如何移除文章图片的宽度和高度属性 2021-12-1714:17:57评论745字 我们在使用
-
8
wordpress学习 移除WordPress上传图片的宽度和高度参数 我们都知道在wordpress后台上传的图片添加到日志内会附带宽高属性。虽然在 HTML...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK