关于微信小程序rich-text中图片宽度超出范围解决办法

问题描述:写一个富文本展示页面时发现一个问题,就是图片宽度超出范围,本能反应是给图片设置一个最大宽度,这里发现不可行。然后又使用正则追加样式还是不行。

// 使用正则提取html里面的图片设置图片最大宽度为100%
res[1].data[0].f_content =res[1].data[0].f_content.replace(/<img[^>]*>/gi, function (match, capture) {
			return match.replace(/style\s*?=\s*?([‘"])[\s\S]*?\1/ig, 
            'style="max-width:100%;height:auto;"') // 替换style
				})

解决方法

这里可以利用属性选择器,给img的src宽度100%

.content_richText [src]{
		max-width: 100%;
	}

解决:
        

猜你喜欢

转载自blog.csdn.net/T3165919332/article/details/133021484