Vue 显示关键词附近内容并高亮显示关键词
创始人
2024-12-26 04:35:27
0

使用v-html显示内容,可识别内容里的标签

  methods:{ 	//获取内容 的关键词附近的内容 	surroundingContent(content,keyword) { 	  const keywordIndex = content.toLowerCase().indexOf(keyword.toLowerCase()); 	  const startIndex = Math.max(0, keywordIndex - 10); 	  const endIndex = Math.min(content.length, keywordIndex + keyword.length + 10); 	  var slicedContent = content.slice(startIndex, endIndex); 	  return this.highlightedContent(slicedContent,keyword); 	}, 	//高亮显示关键词 	highlightedContent(content,keyword) { 	  // 使用正则表达式匹配关键词,并用标签包裹高亮显示 	  const highlighted = content.replace(new RegExp(keyword, 'gi'), match => { 		return `${match}`; 	  }); 	  return highlighted; 	}, 	...... } 
 

解决vue中使用v-html接收后端返回的数据时css样式不能修改的问题
产生问题的原因:由于style里面的scoped,导致v-html里面dom元素的类样式修改不了
解决方案1: 直接在dom的style的行内样式里面写,缺点是一般这个是值是后端直接给你的,行内样式需要拼接,很麻烦。
解决方案2: 在style scoped的下面再写一个style样式,不加scope,专门写这个v-html的样式,需要给v-html里面的dom加一个专门的类,避免全局样式污染到其他页面,因为这个style没有scope。
解决方案3:在style scoped中添加样式穿透:deep(选择器)【推荐使用解决方案3】

相关内容

热门资讯

透视软件!fishpoker透... 大家肯定在之前fishpoker透视辅助器官网入口或者fishpoker透视辅助器官网入口中玩过透视...
hhpoker透视脚本!wep... hhpoker透视脚本!wepoker破解器激活码(透视)竟然是有挂(分享教程)一、wepoker破...
aapoker辅助!微扑克辅助... aapoker辅助!微扑克辅助器ios(透视)本然是有挂(详细辅助玩家交流)1、下载好微扑克辅助器i...
透视智能ai!德扑之星发牌规律... 透视智能ai!德扑之星发牌规律图,wepoker透视真的假的(详细辅助技巧教程);玩家在德扑之星发牌...
hhpoker透视方法!wep... hhpoker透视方法!wepoker轻量版透视方法(透视)其实真的有挂(力荐教程)1)wepoke...
透视透视!wepokerplu... aapoker透视辅助下载赢率提升策略‌;透视透视!wepokerplus开挂,aapoker透视辅...
aapoker透明挂!wepo... aapoker透明挂!wepoke真的有挂(透视)一直有挂(详细辅助推荐十款)是一款可以让一直输的玩...
透视玄学!HHpoker德州有... 透视玄学!HHpoker德州有挂吗,ropalpoker辅助器最新版本更新内容介绍(详细辅助规律教程...
hhpoker开辅助软件!德普... hhpoker开辅助软件!德普之星透视辅助(透视)果然有挂(分享教程)1、德普之星透视辅助系统规律教...
德州之星有辅助挂!德州AI智能... 德州之星有辅助挂!德州AI智能辅助机器人(透视)原生真的有挂(详细辅助重大通报),您好,德州AI智能...