查看: 48|回复: 0

uni-app如何解析html标签

[复制链接]

4

主题

0

回帖

0

积分

热心网友

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2009-3-4
发表于 2022-10-1 23:05:00 | 显示全部楼层 |阅读模式

uni-app如何解析html标签 ,主要包括 uni-app如何解析html标签 使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、导入rechText.js

/*
graceUI rich-text 加强工具
link : graceui.hcoder.net
author : 5213606@qq.com 深海

*/

// 正则变量
var graceRichTextReg;

// 批量替换的样式 [ 根据项目需求自行设置 ]
var GRT = [
    // div 样式
    ['div', "line-height:2em;"],
    // h1 样式
    ['h1', "font-size:3em; line-height:1.5em;"],
    // h2 样式
    ['h2', "font-size:2em; line-height:1.8em;"],
    // h3 样式
    ['h3', "font-size:1.6em; line-height:2em;"],
    // h4 样式
    ['h4', "font-size:1.2em; line-height:2em;"],
    // h5 样式
    ['h5', "font-size:1em; line-height:2em;"],
    // h6 样式
    ['h6', "font-size:0.9em; line-height:2em;"],
    // p 样式
    ['p', "font-size:1em; line-height:2em;"],
    // b 样式
    ['b', "font-size:1em; line-height:2em;"],
    // strong 样式
    ['strong', "font-size:1em; line-height:2em;"],
    // code 样式
    ['code', "font-size:1em; line-height:1.2em; background:#F6F7F8; padding:8px 2%; width:96%;"],
    // img 样式
    ['img', "width:100%; margin:8px 0;"],
    // blockquote
    ['blockquote', "font-size:1em; border-left:3px solid #D1D1D1; line-height:2em; border-radius:5px; background:#F6F7F8; padding:8px 2%;"],
    // li 样式
    ['ul', "padding:5px 0; list-style:none; padding:0; margin:0;"],
    ['li', "line-height:1.5em; padding:5px 0; list-style:none; padding:0; margin:0; margin-top:10px;"],
    // table
    ['table', "width:100%; border-left:1px solid #F2F3F4; border-top:1px solid #F2F3F4;"],
    ['th', "border-right:1px solid #F2F3F4; border-bottom:1px solid #F2F3F4;"],
    ['td', "border-right:1px solid #F2F3F4; border-bottom:1px solid #F2F3F4; padding-left:5px;"]
];


module.exports = {
    format : function(html){
        html = html.replace(/<pre.*pre>?/gis, function(word){
            word =  word.replace(/[\n]/gi,'<br />');
            word =  word.replace(/    /gi,'<span style="padding-left:2em;"></span>');
            return word.replace(/[\t]/gi, '<span style="padding-left:2em;"></span>');
        });
        html = html.replace(/<pre/gi, '<p style="font-size:1em; margin:12px 0; line-height:1.2em; background:#F6F7F8; border-radius:5px; padding:8px 4%; width:92%;"');
        html = html.replace(/<\/pre/gi,"</p");
        for(let i = 0; i < GRT.length; i++){
            graceRichTextReg = new RegExp('<'+GRT[0]+'>|<'+GRT[0]+' (.*?)>', 'gi');
            html = html.replace(graceRichTextReg , function(word){
                // 分析 dom 上是否带有 style=""
                if(word.indexOf('style=') != -1){
                    var regIn = new RegExp('<' + GRT[0] + '(.*?)style="(.*?)"(.*?)(/?)>', 'gi');
                    return word.replace(regIn, '<'+ GRT[0] +'$1style="$2 ' + GRT[1] +'"$3$4>');
                }else{
                    var regIn = new RegExp('<' + GRT[0] + '(.*?)(/?)>', 'gi');
                    return word.replace(regIn, '<'+ GRT[0] +'$1 style="' + GRT[1] +'$2">');
                }
            });
        }
        return html;
    }
    
}

 

<br/>   >>  <view style="font-size:1em; line-height:2em;text-align: justify">&nbsp;</view>



来源:https://www.cnblogs.com/Fooo/p/16747973.html
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部