文本图片,v"/>
富文本图片,v
replaceImg.js
export function replaceImg ({str, width=200, height=300}) {str = str.replace(/(<img[^>]*>)|(<img[^>]*><\/img>)/g, function(match, capture){if(match.indexOf('style') < 0 ){// 没有style 就添加一个return match.replace(/<\s*img/, '<img style=""');} else {// 有style 就不做处理 直接返回return match}})console.log("增加style=\"\"后的html字符串:"+str);str = str.replace(/<img[^>]*>/gi, function (match, capture) {return match.replace(/style\s*?=\s*?([‘"])[\s\S]*?\1/ig, 'style="width:100%;height:auto;margin:0 auto; display:block;"') // 替换style})console.log('htmlstr', str)return str
}
使用
import { replaceImg } from './replaceImg'
methods: {getList() {this.html = replaceImg({str: this.html,width: 300,height: 200})}
}
更简单的办法
img{disblay:block !important;margin: 0 auto !important;width: 300px !important;height: 300px !important;
}
更多推荐
富文本图片,v
发布评论