前端开发攻略---根据搜索关键字实现搜索结果列表文字高亮效果,100%匹配关键字。

1、演示

2、说明

网上这类教程大多采用正则表达式进行文本替换,然而,这种方法有时无法完全匹配,或者会因为特殊符号而产生 Bug。本文将提供一种完美的解决方案,确保百分之百匹配文本。

3、函数封装

封装一个函数,进行通用,避免重复写这段代码

const textHighlighting = (textItem, searchText) => {
  const searchTextArr = searchText.split('')
  const textItemArr = textItem.split('')
  const result = textItemArr.map(item => {
    return searchTextArr.includes(item) ? `<span style="color:red;">${item}</span>` : item
  })
  return result.join('')
}
  1. 定义函数 textHighlighting,接收两个参数 textItem 和 searchText,textItem 表示要处理的文本,searchText 表示要搜索并高亮的文本。
  2. 将 searchText 和 textItem 分别转换为字符数组 searchTextArr 和 textItemArr
  3. 使用 map 方法遍历 textItemArr 中的每个字符。
  4. 对于每个字符,检查是否存在于 searchTextArr 中。
  5. 如果存在,则用 <span style="color:red;"> 包裹该字符,以实现红色高亮效果。
  6. 否则保持原样。
  7. 使用 join 方法将处理后的字符数组组合成字符串。
  8. 返回处理后的字符串作为函数的结果。

您可以参考以下两个案例,直接复制该函数到您的项目中即可使用。

4、在Vue中使用

<template>
  <div>
    <input type="text" v-model="searchText" />
    <ul v-if="searchText">
      <li v-for="item in data" :key="item.id" v-html="textHighlighting(item.text, searchText)"></li>
    </ul>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
const searchText = ref('')

const textHighlighting = (textItem, searchText) => {
  const searchTextArr = searchText.split('')
  const textItemArr = textItem.split('')
  const result = textItemArr.map(item => {
    return searchTextArr.includes(item) ? `<span style="color:red;">${item}</span>` : item
  })
  return result.join('')
}

const data = [
  { id: 1, text: '文字高亮' },
  { id: 2, text: '前端实现文字高亮' },
  { id: 3, text: '前端怎么实现文字高亮' },
  { id: 4, text: '实现文字高亮的办法' },
  { id: 5, text: 'Vue实现文字高亮' },
  { id: 6, text: '高亮文字怎么做' },
  { id: 7, text: '在Vue中(文字高亮)怎么做' },
  { id: 8, text: '文字实现高亮效果' },
]
</script>

<style scoped lang="scss"></style>

5、在原生JS中使用

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" />
  </body>
  <script>
    const data = [
      { id: 1, text: '文字高亮' },
      { id: 2, text: '前端实现文字高亮' },
      { id: 3, text: '前端怎么实现文字高亮' },
      { id: 4, text: '实现文字高亮的办法' },
      { id: 5, text: 'Vue实现文字高亮' },
      { id: 6, text: '高亮文字怎么做' },
      { id: 7, text: '在Vue中(文字高亮)怎么做' },
      { id: 8, text: '文字实现高亮效果' },
    ]
    const ipt = document.querySelector('input')
    const textHighlighting = (textItem, searchText) => {
      const searchTextArr = searchText.split('')
      const textItemArr = textItem.split('')
      const result = textItemArr.map(item => {
        return searchTextArr.includes(item) ? `<span style="color:red;">${item}</span>` : item
      })
      return result.join('')
    }
    ipt.addEventListener('input', function () {
      const ul = document.createElement('ul')
      document.body.removeChild(ul)
      if (this.value) {
        data.forEach(item => {
          const li = document.createElement('li')
          li.innerHTML = textHighlighting(item.text, this.value)
          ul.appendChild(li)
        })
        document.body.appendChild(ul)
      }
    })
  </script>
</html>

 

 

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bbamx.

谢谢您

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值