国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

highlight.js 實現(xiàn)搜索關(guān)鍵詞高亮效果

這篇具有很好參考價值的文章主要介紹了highlight.js 實現(xiàn)搜索關(guān)鍵詞高亮效果。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

先看效果:
highlight.js 實現(xiàn)搜索關(guān)鍵詞高亮效果,javascript,前端,開發(fā)語言
更新:增加切換顯示
highlight.js 實現(xiàn)搜索關(guān)鍵詞高亮效果,javascript,前端,開發(fā)語言

折騰了老半天,記錄一下
注意事項都寫注釋了
代碼:

<template>
  <div class="absolute-lt wh-full overflow-hidden p-10">
   
    <div style="width: 200px">
      <el-input v-model="keyword" @input="search"></el-input>
    </div>
    <code>
      <pre v-html="html"></pre>
    </code>
  </div>
</template>
<script setup>
import { onMounted, computed, reactive, ref } from "vue";
import hljs from "highlight.js";
// 這不引入樣式防止干擾高亮顯示
// import "highlight.js/styles/arta.css";
const str = `
Server: cloudflare
Date: Tue, 02 Jan 2024 15:40:15 GMT
Content-Type: text/html
Content-Length:  553
Connection: keep
CF-RAY: 83f419748811fa1a-SJC

<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>cloudflare</center>
</body>
</html>
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->

`;
const html = ref("");
const keyword = ref("");
let saveValue = ref("");

// 注冊自定義語言,防止生成多余標簽匹配
hljs.registerLanguage("custom", function () {
  return {};
});
html.value = saveValue.value = hljs.highlight(str, { language: "custom" }).value;


function search() {
  if (!keyword.value) return (html.value = saveValue.value);
  let reg = new RegExp(keyword.value, "g", "i");
  html.value = saveValue.value.replace(
    reg,
    "<span class='abc'> " + keyword.value + " </span>"
  );
}
</script>

<style lang="less">
span.abc {
  color: red;
}
</style>

更新后代碼:文章來源地址http://www.zghlxwxcb.cn/news/detail-783058.html

<template>
  <div class="absolute-lt wh-full overflow-hidden p-10">
    <!-- <code>
      <pre>{{ str }}</pre>
    </code> -->
    <!-- <pre>
      <code>{{ str }}</code>
    </pre> -->
    <div class="flex">
      <div style="width: 200px">
        <el-input v-model="keyword" @input="search"></el-input>
      </div>
      <div>{{ cur }}/{{ total }}</div>
      <div>
        <el-button @click="pre">上一個</el-button>
        <el-button @click="next">下一個</el-button>
      </div>
    </div>
    <div class="box">
      <code>
        <pre v-html="html"></pre>
      </code>
    </div>
  </div>
</template>
<script setup>
import { onMounted, computed, reactive, ref } from "vue";
import hljs from "highlight.js";
// import "highlight.js/styles/arta.css";
const str = `
Server: cloudflare
Date: Tue, 02 Jan 2024 15:40:15 GMT
Content-Type: text/html
Content-Length:  553
Connection: keep
CF-RAY: 83f419748811fa1a-SJC

<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>cloudflare</center>
</body>
</html>
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
Server: cloudflare
Date: Tue, 02 Jan 2024 15:40:15 GMT
Content-Type: text/html
Content-Length:  553
Connection: keep
CF-RAY: 83f419748811fa1a-SJC

<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>cloudflare</center>
</body>
</html>
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->Server: cloudflare
Date: Tue, 02 Jan 2024 15:40:15 GMT
Content-Type: text/html
Content-Length:  553
Connection: keep
CF-RAY: 83f419748811fa1a-SJC

<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>cloudflare</center>
</body>
</html>
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
`;
const html = ref("");
const keyword = ref("");
let saveValue = ref("");
let total = ref(0);
let cur = ref(0);
let nodeList = ref([]);

hljs.registerLanguage("custom", function () {
  return {};
});
html.value = saveValue.value = hljs.highlight(str, { language: "custom" }).value;
// html.value = saveValue.value = hljs.highlightAuto(str).value;
window.hljs = hljs;
function search() {
  if (!keyword.value) return (html.value = saveValue.value);
  let reg = new RegExp(keyword.value, "g", "i");
  html.value = saveValue.value.replace(
    reg,
    "<span class='abc'>" + keyword.value + "</span>"
  );
  count();
}
function pre() {
  if (cur.value <= 0) {
    cur.value = 0;
  } else {
    cur.value--;
  }

  scorll();
}
function scorll() {
  for (let index = 0; index < nodeList.value.length; index++) {
    const element = nodeList.value[index];
    element.style = index == cur.value ? "color:blue" : "color:red";
  }
  let box = document.querySelector(".box");
  let top = nodeList.value[cur.value].offsetTop;
  let offset = nodeList.value[0].offsetTop;
  box.scrollTop = top - offset;
}
function next() {
  if (cur.value >= nodeList.value.length) {
    cur.value = nodeList.value.length;
  } else {
    cur.value++;
  }

  scorll();
}

function count() {
  setTimeout(() => {
    nodeList.value = document.querySelectorAll("span.abc");

    total.value = nodeList.value.length;
    nodeList.value[cur.value].style = "color:blue";
  }, 300);
}
</script>

<style lang="less">
span.abc {
  color: red;
}
.box {
  height: 300px;
  overflow-y: auto;
}
</style>



到了這里,關(guān)于highlight.js 實現(xiàn)搜索關(guān)鍵詞高亮效果的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務器費用

相關(guān)文章

  • 文本關(guān)鍵詞高亮-vue版本

    、、 ? 、、 ?

    2024年02月13日
    瀏覽(21)
  • 雙方案-基于Mysql 與 ElasticSearch實現(xiàn)關(guān)鍵詞提示搜索與全文檢索

    雙方案-基于Mysql 與 ElasticSearch實現(xiàn)關(guān)鍵詞提示搜索與全文檢索

    就喜歡搞這種不需要怎么費勁的東西,只需要把思路闡述清楚,隨筆性質(zhì)的博文,順手啊,幾乎不用改定就可以當博文發(fā)布出去。 那么,這里的話我們要做的就是實現(xiàn)這個的一個搜索功能,這個前端我就不說了,實現(xiàn)起來起來其實還是容易的,就是費勁。我們主要關(guān)注

    2024年01月18日
    瀏覽(24)
  • 長尾關(guān)鍵詞挖掘軟件-免費的百度搜索關(guān)鍵詞挖掘

    長尾關(guān)鍵詞挖掘軟件-免費的百度搜索關(guān)鍵詞挖掘

    嗨,大家好!今天,我想和大家聊一聊長尾挖掘工具。作為一個在網(wǎng)絡世界里摸爬滾打多年的人,我對這個話題有著一些個人的感悟和見解,希望能與大家分享。 首先,讓我坦白一點,長尾挖掘工具對于我來說真是救命稻草。在我剛開始做網(wǎng)站優(yōu)化和內(nèi)容創(chuàng)作的

    2024年02月09日
    瀏覽(36)
  • 【MIdjourney】鏡頭效果關(guān)鍵詞

    【MIdjourney】鏡頭效果關(guān)鍵詞

    景深(DOF),是指在攝影機鏡頭或其他成像器前沿能夠取得清晰圖像的成像所測定的被攝物體前后距離范圍。鏡頭光圈、鏡頭距離、及焦平面到拍攝物的距離是影響景深的重要因素。 在MIdjourney中,該會使得畫面主體外的背景變得模糊,以突出畫面主體。 鏡頭光暈是指

    2024年01月17日
    瀏覽(41)
  • Elasticsearch的關(guān)鍵詞搜索

    返回給前端的實體類 es對應的實體類 前端傳遞的搜索參數(shù)實體類 controller層 service層接口 service實現(xiàn)類 Springboot啟動類

    2023年04月08日
    瀏覽(30)
  • VIM統(tǒng)計搜索關(guān)鍵詞命令

    :%s/.//gn ? ? ? ?統(tǒng)計字符數(shù) :%s/i+//gn ? ?統(tǒng)計單詞數(shù) :%s/^//n ? ? ? ? ? 統(tǒng)計行數(shù) :%s/keyword//g ? ? ?統(tǒng)計任何地方出現(xiàn)的 \\\"keyword\\\"?? :%s/keyword//gn ? ?統(tǒng)計任何地方出現(xiàn)的 \\\"keyword\\\" :%s/keyword/ :這部分是 Vim 的替換命令的開頭。:%s 表示在整個文件范圍內(nèi)進行替換操作。keyword 是要查

    2024年02月09日
    瀏覽(25)
  • X書關(guān)鍵詞協(xié)議搜索

    搜索接口中的其他java層加密,詳細見: https://codeooo.blog.csdn.net/article/details/122986633

    2024年02月16日
    瀏覽(23)
  • 網(wǎng)站優(yōu)化搜索引擎與關(guān)鍵詞

    網(wǎng)站優(yōu)化搜索引擎與 人們不應該高估搜索引擎的智商。這不利于seo的研究,事實上,搜索引擎是非常愚蠢的,讓我們舉一個非常簡單的例子,你在搜索引擎中輸入“教師”這個詞,搜索引擎就會給出一個準確的搜索列表。我們不會給出“教師”一詞的檢索信息,但我們

    2024年02月09日
    瀏覽(119)
  • 抖音關(guān)鍵詞搜索小程序排名怎么做

    抖音關(guān)鍵詞搜索小程序排名怎么做

    抖音搜索小程序排名怎么做 1 分鐘教你制作一個抖音小程序。 抖音小程序就是我的視頻,左下方這個藍色的鏈接,點進去就是抖音小程序。 如果你有了這個小程序,發(fā)布視頻的時候可以掛載這個小程序,直播的時候也可以掛載這個小程序進行帶貨。 ? 制作小程序一共

    2024年02月13日
    瀏覽(37)
  • Python獲取高德POI(關(guān)鍵詞搜索法)

    Python獲取高德POI(關(guān)鍵詞搜索法)

    該篇文章是搜索法獲取高德poi,但鑒于無法突破900條記錄的上限,因此重寫了 矩形搜索法 的文章,具體可參考以下文章: 高德poi獲取之矩形搜索法(沖出900條限制) (建議沒有python基礎的朋友先閱讀該篇再看矩形搜索法?。?首先我們需要明白一些常識 poi是興趣點,它

    2024年02月06日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包