加载HTML富文本详情

// 使用 rich-text 加载富文本数据
<rich-text class="richText" :nodes="nodes"></rich-text>
一 .对 H5 中的详情
  1. 自适应HTML内容 中的图片大小.
// 全局搜索并替换图片样式
 this.nodes = this.item.content.replace(/<img/gi,'<img style="max-width:100%;height:auto;margin:0 auto;display:block"')
  1. 使用增加权重的方式, 修改 HTML 内容中的字体样式, 统一标签文字样式.
    .richText {
        padding: 0rpx 30rpx 0rpx 30rpx;
        // line-height:1.667em;
        // letter-spacing:0.5px;
        text-align:justify;
        word-break: break-all;
        p{
            font-size: 32rpx !important;
            // text-indent: 2em !important;
            text-align: justify !important;
            color: #333333 !important;
            line-height:1.667em !important;
            letter-spacing:0.5px !important;
            word-break: break-all !important;
            // margin-bottom: 28rpx !important;
        }
        span{
            font-size: 32rpx !important;
            text-indent: 2em !important;
            text-align: justify !important;
            color: #333333  !important;
            line-height:1.667em !important;
            letter-spacing:0.5px !important;
            word-break: break-all !important;
        }
    }
二. 对于微信小程序

.1. 自适应HTML内容 中的图片大小.

// content 为后端返回的富文本内容
formatImg(content) {
    if (!content) return '';
        const regex = /<img[^>]*>/g;
        content = content.replace(regex, function(match) {
        return match.replace('>', 'style="max-width:100%;height:auto;">');
        });
    return content;
},
  1. 直接使用class增加权重的方式, 修改 HTML 内容中的字体样式, 统一标签文字样式
    .richText {
        padding: 0rpx 30rpx 0rpx 30rpx;
        // line-height:1.667em;
        // letter-spacing:0.5px;
        text-align: justify;
        word-break: break-all;

        font-size: 32rpx !important;
        // text-indent: 2em !important;
        text-align: justify !important;
        color: #333333 !important;
        line-height: 1.667em !important;
        letter-spacing: 0.5px !important;
        word-break: break-all !important;
        // color: red !important;
        // margin-bottom: 28rpx !important;
    }
三. 实现图片和文件的监听

查看官网
我们通过查看官网属性可知, 有个@itemclick事件,可以拦截点击事件(只支持 a、img标签),返回当前node信息 event.detail={node}
具体实现:
注意: @itemclick 只是对 H5 页面有效, 小程序是监听不到的

  <rich-text space="emsp" :nodes="nodes" @itemclick="handleItemClick" ></rich-text>

  /*  item事件处理  */
handleItemClick(e) {
    const type = e.detail.node.name
    if (type == 'a') {
    // 附件下载预览
    let url =  e.detail.node.attrs.href
    let title = e.detail.node.attrs.title
    uni.downloadFile({
        url: url,
        success: (res) => {
            if (res.statusCode === 200) {
                    uni.hideLoading()
                    let filePath = res.tempFilePath;
                    uni.openDocument({
                        filePath: filePath,
                        showMenu: true,
                        success: (res) => {
                            console.log('打开文档成功');
                            },
                          fail: (err) => {
                                console.log('打开失败', err);
                        }
                });
            }
        },
          fail: (err) => {
            uni.hideLoading()
            console.log('下载失败', err);
        }
    });
} else if (type == 'img') {
    //  图片预览
    let url = e.detail.node.attrs.src
    uni.previewImage({
        urls: [url],
        longPressActions: {
        itemList: [],
        success: function(data) {
            // 处理长按保存操作
              }
          }
         })
    } else {
    // 其他类型
    console.log("======= 其他类型:", type)
    }
},

四. 使用 u-parse 在 H5 和小程序段都可以使用, 一劳永逸.

文档地址
图片会自适应.

如果是简单的场景,比如一段简单的文字和图片内容,可以优先使用rich-text组件,在文章内容,商品详情等复杂的文本详情,可以优先使用parse组件。

<u-parse :content="nodes" :tagStyle="style" @imgTap="imgTap" @linkTap="linkTap" :copy-link="false"></u-parse>

// 设置样式
style: {
          // 字符串的形式
          p: "color: red;font-size:32rpx",
          span: "font-size: 30rpx",
      },

// 图片被点击时触发
imgTap() {

}

// 链接被点击时触发
linkTap() {

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,696评论 1 92
  • 本文主要讲述页面布局样式方面涉及的知识点,更全面的对CSS相应的技术进行归类、整理、说明,没有特别详细的技术要点说...
    Joel_zh阅读 4,704评论 0 1
  • 测试文本数据 //html标签图片自适应 NSString *detailTextString = [STNews...
    小涛_78e8阅读 4,993评论 2 5
  • HTML总结 sublime 环境配置 使用sublime text 3 打开百度在搜索框中输入sublime t...
    Viarotel阅读 3,321评论 0 0
  • 前端开发面试题 面试题目: 根据你的等级和职位的变化,入门级到专家级,广度和深度都会有所增加。 题目类型: 理论知...
    怡宝丶阅读 7,406评论 0 7

友情链接更多精彩内容