搜索高亮显示

这篇博客介绍了一种实现搜索框内容高亮的方法。通过将搜索结果拆分成单个字符,与输入框内容比对,设置标志位来实现高亮显示。在WXML中,使用`<input>`和`<view>`组件结合图标展示搜索框,并在WXSS中定义了高亮样式。JS部分的代码处理了搜索列表,当输入字符串包含某项的名称时,该项会被标记为高亮。这种方法有助于提高用户在搜索列表中查找目标的效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

效果如图所示

思路就是,把搜索出来的内容切割成一个个字,然后找搜索框里面的字,如果一样的话,flag设置为true

下面直接贴代码吧

wxml

  <view class="input {{flag?'':'eight'}}">
    <icon type="search" size="23" color=""></icon>
    <input type="text" placeholder="请输入" bindinput="bindKeyInput" bindfocus='bindfocus' value="{{value}}" />
  </view>
  <view wx:for='{{searchList}}' bindtap='choosePhone' data-name='{{item.name}}' data-phone='{{item.phone}}'>
  	<view>
  		<view class="name">
  			<text class="{{a.flag?'blue':''}}" wx:for='{{item.aa}}' wx:for-item='a'>{{a.name}}</text>
  		</view>
  		<view class="unit">{{item.unit}}</view>
  	</view>
  	<view style="color: black;font-size: 32rpx;">{{item.phone}}</view>
  </view>

wxss

.blue {
  color: #25AFF6 !important;
  font-weight: bold;
}

js

// 高亮的代码
let list = res.result
let name = []
for (let [index, s] of list.entries()) {
  let aa = []
  name = s.name.split('')
  for (let [index, n] of name.entries()) {
    aa.push({
      name: n,
      flag: false
    })
    s.aa = aa
  }
}


// 高亮的代码
let listr = that.data.searchList
for (let i of listr) {
	for (let s of i.aa) {
		if (str.indexOf(s.name) !== -1) {
			s.flag = true
			console.log(listr);
			that.setData({
				searchList: listr
			})
		} else {
			s.flag = false
			that.setData({
				searchList: listr
			})
		}
	}
}

 

React全局搜索高亮显示是一种常见的功能,它可以在React应用中实现对文本内容的搜索,并将匹配到的结果进行高亮显示。下面是一种实现方式: 1. 创建一个SearchHighlight组件,用于接收搜索关键词和文本内容作为props。 2. 在SearchHighlight组件中,使用正则表达式将搜索关键词在文本内容中进行匹配,并将匹配到的部分用<span>标签包裹起来,并添加一个特定的类名用于样式控制。 3. 在父组件中,通过state来保存搜索关键词,并将其传递给SearchHighlight组件。 4. 监听搜索关键词的变化,在每次变化时重新渲染SearchHighlight组件。 下面是一个简单的示例代码: ```jsx import React, { useState } from 'react'; const SearchHighlight = ({ keyword, text }) => { const regex = new RegExp(`(${keyword})`, 'gi'); const parts = text.split(regex); return ( <span> {parts.map((part, index) => part.toLowerCase() === keyword.toLowerCase() ? ( <span key={index} className="highlight"> {part} </span> ) : ( part ) )} </span> ); }; const App = () => { const [searchKeyword, setSearchKeyword] = useState(''); const handleSearchChange = (e) => { setSearchKeyword(e.target.value); }; return ( <div> <input type="text" value={searchKeyword} onChange={handleSearchChange} /> <SearchHighlight keyword={searchKeyword} text="Lorem ipsum dolor sit amet, consectetur adipiscing elit." /> </div> ); }; export default App; ``` 在上述代码中,SearchHighlight组件接收两个props:keyword和text。它使用正则表达式将keyword在text中进行匹配,并将匹配到的部分用<span>标签包裹起来,并添加了一个highlight类名用于样式控制。 在父组件App中,使用useState来保存搜索关键词searchKeyword,并通过input的onChange事件来更新searchKeyword的值。每次searchKeyword的值发生变化时,SearchHighlight组件会重新渲染,实现了全局搜索高亮显示的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值