赞
踩
方式一:在style中追加 max-width:100%;height:auto; ,此方法优点灵活,对于富文本中所有图片都起作用
- // 商品详情html正则追加max-width
- textImageConvert(content){
- return content.replace(/<(img).*?(\/>|<\/img>)/g, function (mats) {
- if (mats.indexOf('style') < 0) {
- return mats.replace(/<\s*img/, '<img style="max-width:100%;height:auto;"');
- } else {
- return mats.replace(/style=("|')/, 'style=$1max-width:100%;height:auto;')
- }
- });
- }
方式二:这种方式会替换之前设置的样式
-
- textImageConvert(content) {
- return content.replace(/<img[^>]*>/gi, function (match, capture) {
- return match.replace(/style\s*?=\s*?([‘"])[\s\S]*?\1/ig, 'style="max-width:100%;height:auto;"') // 替换style
- });
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。