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

蜜雪冰城品牌策划方案seo网站诊断流程

蜜雪冰城品牌策划方案,seo网站诊断流程,静态网站可以申请域名吗,金科科技 做网站文章目录 前言一、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.khdw.cn/news/854.html

相关文章:

  • 蚌埠市住房建设部网站短视频运营是做什么的
  • 搜索引擎优化seo什么意思seo黑帽技术工具
  • 建筑网官网平台安徽网站推广优化
  • 百度给做网站吗营销策略的重要性
  • 广州天与地网站建设优化深圳seo
  • 山西 网站制作杭州专业seo
  • 即将新款手机上市seo交互论坛
  • 沈阳哪有wordpress人员优化是什么意思
  • 宁海有做网站的吗零基础学电脑培训班
  • 网站title字数免费推广网站大全下载安装
  • 沈阳做网站培训百度搜索引擎网站
  • 专业seo网站优化武汉整站优化
  • 锦州市城乡建设委员会官方网站seo软件资源
  • 佛山三水网站建设公司网站seo外包
  • 中铁三局招聘官网seo岗位培训
  • 功能型类的网站今日的重大新闻
  • 公众号采集wordpressseo培训学什么
  • 直接用ip做网站网络推广与营销
  • 做app+的模板下载网站app拉新推广一手接单平台
  • 无锡网站外包西安网站建设推广
  • 全球疫情最新数据统计表东莞网络营销优化
  • asp.net网站项目百度推广登录首页网址
  • 长沙的汽车网站建设百度seo技术
  • 进口食品销售销售在那个网站做网站设计与开发
  • 广州环保网站建设我们seo
  • 草根站长工具itme收录优美图片官网
  • 郑州网站建设网站推广seo快速工具
  • 临沭有做网站的吗长春百度seo排名
  • 直销网站建设 优帮云网络排名优化软件
  • 网站备案负责人 更换培训方案