当前位置: 首页 > news >正文

网站开发与建设课程湖南好搜公司seo

网站开发与建设课程,湖南好搜公司seo,响应式网站css,深度网络科技有限公司文章目录 前言一、video的处理二、img的处理总结 前言 小程序中使用富文本编辑器,由于rich-text受限 部分富文本内容无法渲染或排版错乱。以img和video为例,处理起来让人头疼。网上各种长篇大论,实际上没有任何帮助。接下来我们就一起聊聊im…

文章目录

  • 前言
  • 一、video的处理
  • 二、img的处理
  • 总结


前言

小程序中使用富文本编辑器,由于rich-text受限 部分富文本内容无法渲染或排版错乱。以img和video为例,处理起来让人头疼。网上各种长篇大论,实际上没有任何帮助。接下来我们就一起聊聊img和video。


一、video的处理

因为rich-text不识别video,所以小程序端无法渲染。网上看很多都是下载插件处理,如果是一个单独视频 我个人觉得用正则匹配出来就行了,如果是富文本里面还穿插其他内容,可以试试插件。近期我们做的需求是把富文本里面的video拿出来单独渲染。
那么接下来我们就看看单独渲染如何处理吧。
此处是部分核心代码 并非完整案例代码

let htmlStr = '<p>测试题目解析的东西</p>↵<p><video controls="controls" width="300" height="150">↵<source src="https://scsf.oss-cn-shanghai.aliyuncs.com/tk/4d896a6e-f467-4a6d-bbc0-e30b5135db2f/bg.mp4" type="video/mp4"></video></p>'
let videoList = formatVideo(htmlStr )
const formatVideo = (htmlStr )=>{if (!htmlStr ) return []let srcReg = /(?<=(source[^>]src="))[^"]*/glet videoList = htmlStr.match(srcReg) || []return videoList
}

二、img的处理

说起img的处理 网上各种版本,网上大部分都是直接暴力 设置max-width跟height auto;看到后只想吐槽一句,‘兄弟,你这是用到实战项目上吗’ 。
img处理 包含三种情况
第一种 不带style
例如

let htmlStr = '<img src="...." />'

第二种 带style不包含width
例如

let htmlStr = '<img style="text-align:center" src="...."'

第三种 带style并且有style
例如

let htmlStr = '<img style="width:100px;height:30px;" src="...." />'

所以针对以上情况 分别进行处理就行。
第一步 判断是否包含style属性

 let regExp = new RegExp('(i?)(<img)(?!(.*?style=[\'"](.*)[\'"])[^>]+>)', 'gmi')

第二步 取出style属性值。

let srcReg = /(?<=(<img[^>]style="))[^"]*/g

第三步 判断是否包含width
完整代码

const formatRichTextInfo = (str, width) => {var regExp = new RegExp('(i?)(<img)(?!(.*?style=[\'"](.*)[\'"])[^>]+>)', 'gmi')let result = ''if (regExp.test(str)) {result = str.replace(/\<img/gi, '<img style="max-width:100%;height:auto;"')} else {let srcReg = /(?<=(<img[^>]style="))[^"]*/glet attributeList = str.match(srcReg) || []if (attributeList && attributeList .length) {if (attributeList [0].indexOf('width') != -1) {result = str.replace(/(<img[^>]*style="[^"]*?)(\bwidth\s*:\s*\d+[^;"]*?px;)(\s?height\s*:\s*\d+[^;"]*?px;)([^<]*\/>)/gi,function (match, p1, p2, p3, p4) {var widthValue = parseInt(p2.match(/\d+/)[0])// var heightValue = parseInt(p3.match(/\d+/)[0])if (widthValue > width) {return p1 + `width: ${width}px; height: auto;` + p4}return match // 如果width小于等于375,则不做替换,保持原样})} else {result = str.replace(/<img[^>]*>/gi, function (match, capture) {match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '')return match})result = result.replace(/\<img/gi, '<img style="max-width:100%;height:auto;"')result = result.replace(/\<img/gi, '<img style="max-width:100%;height:auto;"')}} else {result = str.replace(/<img[^>]*>/gi, function (match, capture) {match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '')return match})//再设置新的样式result = result.replace(/\<img/gi, '<img style="max-width:100%;height:auto;"')result = result.replace(/\<img/gi, '<img style="max-width:100%;height:auto;"')}}return result
}

总结

不管是video还是img的处理 都是利用正则进行匹配。
img稍微麻烦一点 分为三种情况 没有style直接替换 有style则把style属性全部取出,然后判断是否包含width属性 如果包含 则需要给一个最大值进行替换 如果不包含 则把style重置 然后再手动赋个最大值。

http://www.tj-hxxt.cn/news/119599.html

相关文章:

  • wordpress 路由自定义页面西安seo优化培训
  • 聊天网站制作教程企业网站seo点击软件
  • 优秀的国外设计网站谷歌google下载安卓版 app
  • 福州网站建设服务价格最实惠搜索引擎优化seo专员
  • html5 国内网站建设seo教程
  • 自己做网站建议seo黑帽技术
  • 延吉网站建设优化师
  • 中国机械加工网加热炉节能西安网站seo哪家公司好
  • 睢宁县凌城做网站的如何在百度推广
  • flash做的网站青岛seo青岛黑八网络最强
  • 装饰设计网站建设百度seo正规优化
  • 网站怎么做三级的百度推广下载安装
  • 手机怎么做网站教程磁力宅
  • 计算机做网站开发需要什么证书石家庄抖音seo
  • 乐享视频在线下载免费台州关键词首页优化
  • 江苏省城乡建设厅网站首页seo推广公司排名
  • 有没有个人做的网站赚流量费推广学院seo教程
  • 怎么做网站关键字搜索seo学校
  • 北京网站关键词排名推广怎么样优化关键词排名
  • 中移建设 公司 网站网站策划是什么
  • 如何设计酒店网站建设seo是什么单位
  • 成都网站建设xh web谷歌seo优化排名
  • 做购物网站的开题报告营销软件站
  • 手机网站跟pc网站有什么不同seo 专业
  • 福州做网站建设北京网络seo
  • 南昌自主建站模板做网站的公司
  • 河间市网站建设网站流量统计分析
  • b2b网站程序营销型网站建设专家
  • 做旅游网站多少钱ai智能搜索引擎
  • 哪些网站需要备案蚌埠网络推广