在开发时,将富文本编辑器中的内容渲染到页面,需要对图片样式进行修改,尝试了直接更改样式,发现并没有效果,这是因为原有富文本编辑器中的图片原有样式级别更高,后面加的样式不能够覆盖原有的样式,需要先将元素原有样式清除,再重新增加新的样式,代码如下:
//获取页面详情
getClubInfo() {
getClubInfo(that.id).then(res => {
that.store_info = res.data.re_data
const regex = new RegExp('<img', 'gi')
// 先清除原有样式
let newhtml = res.data.re_data.i_content.replace(/<img[^>]*>/gi,
function(match,capture) {
match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi,'');
return match;
});
//再设置新的样式
this.i_content = newhtml.replace(regex, `<img style="max-width: 100%; height:auto;"`)
}
},