vue實(shí)現(xiàn)文字檢索時(shí)候?qū)⑺阉鲀?nèi)容標(biāo)紅功能
實(shí)現(xiàn)結(jié)果

html:
<div class="searchBox">
<span class="bt">標(biāo)  題</span>
<div class="search">
<div class="shuru">
<!-- <span class="title">生產(chǎn)經(jīng)營(yíng)</span> -->
<input type="請(qǐng)輸入要查詢的內(nèi)容" 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: '',
}), //搜索內(nèi)容的數(shù)據(jù)
async getSearch(val) {
console.log(val, this.searchText);
this.$nextTick(()=>{
this.getIsmDatas()
})
},
//獲取需要標(biāo)紅的文字
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;
},
//將文字標(biāo)紅
brightenKeyword(contentText, keyword) {
// debugger
var res = contentText
// keyword = keyword.replace("+",'')
var judgeFn = new RegExp(/\s+/g); //空格的正則
//有加號(hào)的時(shí)候
if (keyword.includes('+')) {
// const Reg = new RegExp("+", 'g');
keyword = keyword.replace("+", '')
}
//有空格的時(shí)候
else if (judgeFn.test(keyword)) {
console.log(keyword, "【結(jié)果】:內(nèi)容包含有空格!");
let wordsArray = this.getRedWords(contentText, keyword);
for (let word of wordsArray) {
if (word != "") {
// 設(shè)定需要檢索的模式
const Reg = new RegExp(word, 'g');
//替換每一個(gè)相同字
res = res.replace(Reg, `<span style="color: red; font-family: 'pfm';">${word}</span>`);
}
}
return res
}
// 判斷標(biāo)題中是否包含關(guān)鍵字
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; //此時(shí)的res里已經(jīng)將需要標(biāo)紅的字體帶上了格式(<span style="color:red"></span>)
},
//獲取表格數(shù)據(jù)
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.data
this.PageInfo.total = res.data.data.count
}
},到此這篇關(guān)于vue中實(shí)現(xiàn)文字檢索時(shí)候?qū)⑺阉鲀?nèi)容標(biāo)紅的文章就介紹到這了,更多相關(guān)vue搜索內(nèi)容標(biāo)紅內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue-treeselect無(wú)法點(diǎn)擊問(wèn)題(點(diǎn)擊無(wú)法出現(xiàn)拉下菜單)
這篇文章主要介紹了vue-treeselect無(wú)法點(diǎn)擊問(wèn)題(點(diǎn)擊無(wú)法出現(xiàn)拉下菜單),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue中用 async/await 來(lái)處理異步操作
這篇文章主要介紹了vue中用 async/await 來(lái)處理異步操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
vuex之this.$store.dispatch()與this.$store.commit()的區(qū)別及說(shuō)明
這篇文章主要介紹了vuex之this.$store.dispatch()與this.$store.commit()的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
uniapp Vue3中如何解決web/H5網(wǎng)頁(yè)瀏覽器跨域的問(wèn)題
存在跨域問(wèn)題的原因是因?yàn)闉g覽器的同源策略,也就是說(shuō)前端無(wú)法直接發(fā)起跨域請(qǐng)求,同源策略是一個(gè)基礎(chǔ)的安全策略,但是這也會(huì)給uniapp/Vue開發(fā)者在部署時(shí)帶來(lái)一定的麻煩,這篇文章主要介紹了在uniapp Vue3版本中如何解決web/H5網(wǎng)頁(yè)瀏覽器跨域的問(wèn)題,需要的朋友可以參考下2024-06-06
解決vue3項(xiàng)目打包發(fā)布到服務(wù)器后訪問(wèn)頁(yè)面顯示空白問(wèn)題
這篇文章主要介紹了解決vue3項(xiàng)目打包發(fā)布到服務(wù)器后訪問(wèn)頁(yè)面顯示空白問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Vuejs仿網(wǎng)易云音樂(lè)實(shí)現(xiàn)聽歌及搜索功能
這篇文章主要介紹了Vuejs仿網(wǎng)易云音樂(lè)實(shí)現(xiàn)聽歌及搜索功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
vue項(xiàng)目實(shí)現(xiàn)面包屑導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了vue項(xiàng)目實(shí)現(xiàn)面包屑導(dǎo)航,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
vue cli 3.x 項(xiàng)目部署到 github pages的方法
這篇文章主要介紹了vue cli 3.x 項(xiàng)目部署到 github pages的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-04-04

