在开发时,将富文本编辑器中的内容渲染到页面,需要对图片样式进行修改,尝试了直接更改样式,发现并没有效果,这是因为原有富文本编辑器中的图片原有样式级别更高,后面加的样式不能够覆盖原有的样式,需要先将元素原有样式清除,再重新增加新的样式,代码如下:
//获取页面详情
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;"`)
}
},
本文讲述了在开发过程中,如何处理富文本编辑器内容渲染时图片样式的问题。由于原有样式级别高,直接修改无效,需先清除元素样式,再应用新的CSS样式。作者给出了使用JavaScript操作DOM的示例代码。
3325





