• Vue 显示关键词附近内容并高亮显示关键词


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

    <div v-html="surroundingContent(blog.content,input)">div>
    
      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】

  • 相关阅读:
    HTML做一个简单漂亮的宠物网页(纯html代码)
    mybatis-plus技巧--动态表名-多语句-拼接sql--关于mybatis的mysql分页查询总数的优化思考
    c++系列之vector类模拟实现
    面向对象方法学:对象
    MySQL数据库基本操作+用户管理+用户授权
    解密网络世界的秘密——Wireshark Mac/Win中文版网络抓包工具
    第4章 8088/8086指令系统
    Linux 忘记密码解决方法
    API接口大全:常用、热门、免费的都有
    为什么手动采购管理会危及你的工作流程?
  • 原文地址:https://blog.csdn.net/heiye_007/article/details/140462839