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

vue中实现文字检索时候将搜索内容标红

实现结果

 html:

 <div class="searchBox"><span class="bt">标&#8195&#8195题</span><div class="search"><div class="shuru"><!-- <span class="title">生产经营</span> --><input type="请输入要查询的内容" v-model="searchText" @@keydown.enter="getSearch('btn')"></div><div class="btn" @click="getSearch('btn')"><img src="../../assets/wyc/search.png" alt=""></div></div></div> <span v-html="brightenKeyword(el.filename,searchText)"></span>

搜索框样式:

.searchBox {height: 70px;display: flex;align-items: center;.bt {font-family: 'pf';font-size: 18px;color: #000;font-weight: 400;margin-right: 45px;}.search {width: 756px;height: 46px;display: flex;position: relative;.shuru {width: 680px;height: 46px;background: #FFFFFF;border: 1px solid rgba(12, 174, 149, 1);border-radius: 8px 0px 0px 8px;box-sizing: border-box;display: flex;align-items: center;justify-content: space-between;padding: 0 20px;.bt {white-space: nowrap;display: inline-block;}.title {font-family: 'pf';font-size: 16px;color: #333333;font-weight: 400;padding-right: 7px;white-space: nowrap;border-right: 1px solid #D8D8D8;margin-right: 12px;}input {width: 99%;height: 34px;box-sizing: border-box;border: none;outline: none;font-family: 'pf';font-size: 16px;color: #333333;font-weight: 400;white-space: nowrap;}}.btn {width: 78px;height: 46px;background: #0CAE95;border-radius: 0px 8px 8px 0px;display: flex;align-items: center;justify-content: center;img {width: 22px;height: 22px;}}.tips {width: 274px;display: flex;// align-items: center;position: absolute;right: -280px;top: 50%;transform: translateY(-50%);img {width: 18px;height: 18px;}span {font-family: 'pf';font-size: 12px;color: #7a7a7a;font-weight: 400;white-space: wrap;margin-left: 3px;display: none;}&:hover {span {display: block;}}}}
}

JavaScript: 

    data: () => ({searchText: '',}),
        //搜索内容的数据async getSearch(val) {console.log(val, this.searchText);this.$nextTick(()=>{this.getIsmDatas()})},//获取需要标红的文字getRedWords(contentText, keyword) {let keywordArray = keyword.split(" ");console.log(keywordArray, '有空格的字...');let wordsArray = [];for (let key of keywordArray) {if (contentText.includes(key)) {wordsArray.push(key)}}// this.changeRedText(contentText,wordsArray)return wordsArray;},//将文字标红brightenKeyword(contentText, keyword) {// debuggervar res = contentText// keyword = keyword.replace("+",'')var judgeFn = new RegExp(/\s+/g); //空格的正则//有加号的时候if (keyword.includes('+')) {//    const Reg = new RegExp("+", 'g');keyword = keyword.replace("+", '')}//有空格的时候else if (judgeFn.test(keyword)) {console.log(keyword, "【结果】:内容包含有空格!");let wordsArray = this.getRedWords(contentText, keyword);for (let word of wordsArray) {if (word != "") {// 设定需要检索的模式const Reg = new RegExp(word, 'g');//替换每一个相同字res = res.replace(Reg, `<span style="color: red; font-family: 'pfm';">${word}</span>`);}}return res}// 判断标题中是否包含关键字if (contentText.includes(keyword)) {const Reg = new RegExp(keyword, 'g');res = res.replace(Reg, `<span style="color: red; font-family: 'pfm';">${keyword}</span>`);}return res; //此时的res里已经将需要标红的字体带上了格式(<span style="color:red"></span>)},//获取表格数据async getIsmDatas() {let res = await this.$http.get(`/ctrl/ismStudy/getIsmDatas?unitType=${this.unitType}&jobnature=${this.tabOption['code_value']}&jobnatureText=${this.tabOption['code_name']}&page=${this.PageInfo.pagenum}&limit=${this.PageInfo.pagesize}`)if (res && res.status == 200) {this.tableData.tbody = res.data.data.datathis.PageInfo.total = res.data.data.count}},

http://www.lryc.cn/news/129766.html

相关文章:

  • PCL protocol composition logic
  • 聊聊看React和Vue的区别
  • OSPF在广播类型的网络拓扑中DR和BDR的选举
  • 系统学习Linux-Mariadb高可用MHA
  • 慢SQL的原因
  • php正则替换文章的图片
  • 57 | TAPTAP客户端分析
  • 开源了一套基于springboot+vue+uniapp的商城,包含分类、sku、商户管理、分销、会员、适合企业或个人二次开发
  • Android进阶之多级列表
  • Stochastic: Distribution-Expectation-Inequalities
  • Java算法_ 二叉树的最大深度(LeetCode_Hot100)
  • 行业追踪,2023-08-18
  • js将项目中的图片上传到服务器
  • 【C语言】指针的进阶
  • 【Windows系统编程】03.远线程注入ShellCode
  • 第1天----验证一个字符串是否是另一个字符串的子串
  • 项目实战第四十三讲:使用模版模式优雅实现财务编辑费用
  • [JavaWeb]【六】web后端开发-请求响应
  • uniapp websocket机制 心跳 重连
  • labelme安装以及标注自己的目标检测数据集
  • 归并排序JS
  • matlab 计算点云平均密度
  • c++ | uint8_t、uint16_t、unint32_t 和size_t
  • mysql 超大 sql 文件导入过程
  • 【悬挂绝缘子的串效模型】测量每个绝缘子盘之间的电压并测量串效研究(Simulink)
  • P3957 [NOIP2017 普及组] 跳房子
  • VR数字工厂多元化展现,打造数字企业工厂名片
  • uniapp封装组件,选中后右上角显示对号√样式(通过css实现)
  • 华为OD面试(部分)
  • 从零做软件开发项目系列之一综论软件项目开发