uniapp 富文本图片100%显示

摘要:
以下方法在百度上找到,但它总是报告错误,原因也没有找到。我简单地逐一检查,发现在过滤之前需要检查传递的值。只有当它是字符串时,此方法才能执行过滤器:,以下是完整的方法筛选器:{formatRichText{//如果{//检测到的值是字符串newContent=html.replace;newContent=newContent.replace;newContent=newContent;newContent=newContent.replace;newContent=enewContent.release;newContent=newContent.lacement;newContent=newContent.lace;returnnewContent;}},

下面的这个方法是在百度上找的,但是老是报错,一直没找到原因,索性自己就挨个检查了下,发现在过滤之前需要对传过来的值进行检测,是字符串才能让这个方法执行

filters: {
    formatRichText(html) { //控制小程序中图片大小
        let newContent = ''
            newContent = html.replace(/<img[^>]*>/gi,
            function(match) {
                match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
                match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
                match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
                return match;
            });
            newContent = newContent.replace(/style="[^"]+"/gi,
            function(match) {
                match = match.replace(/[^;]+;/gi, 'max-100%;').replace(/[^;]+;/gi, 'max-100%;');
                return match;
            });
            newContent = newContent.replace(/<br[^>]*/>/gi, '');
            newContent = newContent.replace(/<img/gi, '<img style="max-100%;height:auto;display:inline-block;margin:10rpx auto;"');
            return newContent;

        

    }
},

以下为完整方法

filters: {
    formatRichText(html) { //控制小程序中图片大小
        let newContent = ''
        if (typeof(html) == 'string') { //检测值为字符串才能进行replace操作
            newContent = html.replace(/<img[^>]*>/gi,
            function(match) {
                match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
                match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
                match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
                return match;
            });
            newContent = newContent.replace(/style="[^"]+"/gi,
            function(match) {
                match = match.replace(/[^;]+;/gi, 'max-100%;').replace(/[^;]+;/gi, 'max-100%;');
                return match;
            });
            newContent = newContent.replace(/<br[^>]*/>/gi, '');
            newContent = newContent.replace(/<img/gi, '<img style="max-100%;height:auto;display:inline-block;margin:10rpx auto;"');
            return newContent;

        }

    }
},

免责声明:文章转载自《uniapp 富文本图片100%显示》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇(转)C++类所占内存大小计算谷粒商城安装ES及入门(十六)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

lambda表达式转换sql

usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Linq; usingSystem.Linq.Expressions; usingSystem.Text; usingSystem.Threading.Tasks; namespace...

sql server中字符串无法替换空格的问题

直接上代码: select case when 'workReport'=LTRIM(RTRIM(' workReport ')) then 'trim去空格成功' when 'workReport'=REPLACE(' workReport ',' ','') then 'replace去空格成功' when 'workReport'=REPLACE('...

leetcode 36 有效的数独 哈希表 unordered_set unordersd_map 保存状态 leetcode 37 解数独

leetcode 36 感觉就是遍历。 保存好状态,就是各行各列还有各分区divide的情况 用数组做。 空间小时间大 class Solution { public: bool isValidSudoku(vector<vector<char>>& board) { int row[9][9]={...

Amazon教程:刚买就降价!避免损失,申请PRICE MATCH(价格保护)的方法

Amazon的商品价格波动频繁,虽然老白通常都在价格较低的时机向大家推荐,但是经常有降了又降的情况,刚下的单还没到手就又降价了,这种滋味肯定不好受。Amazon客服明确告诉老白一周内降价都可以申请PRICE MATCH(价格保护),会很爽快的给予补偿。以下老白就给大家介绍如何通过LIVE CHAT向客服申请价格保护。 另外,只有Amazon自营(Sold...

easyui textarea回车导致datagrid 数据无法展示的问题

textarea换行 在easyui中的datagrid中使用行内编辑时textarea的换行保存到mysql数据库为 在textarea中输入回车符 在js读取textarea中的值有 然后到业务层转换到string中就有可能变成空格形式然后被存入数据库,当在取出此值的时候则会变成空格的形式,因此我们需要将不显示的 替换一下。  在js取textarea...

经验解决Fragment被Replace后仍旧可见的问题

经验解决Fragment被Replace后仍旧可见的问题 网上问的问题,大多会提到替换了Fragment而发现之前被替换的仍旧显示在那里。我个人使用android 2.3 +support 开发包,在2.3系统上也出现类似问题。搜了下网上的问题,好像都没有找到解决方法。之后自己摸索。最后发现其实,对于Fragment的替换 JAVA代码基本上没啥,网上都是...