国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 > 常見問題 > 正文

jquery中什么是高亮顯示

betcha
發(fā)布: 2023-06-13 10:12:51
原創(chuàng)
1883人瀏覽過
jQuery中高亮顯示是指對頁面搜索關(guān)鍵詞時進(jìn)行高亮顯示,其實(shí)現(xiàn)辦法:1、先獲取要高亮顯示的行,獲取搜索的內(nèi)容,再遍歷整行內(nèi)容,最后添加高亮顏色;2、使用“jQuery highlight”高亮插件,官網(wǎng)下載js文件后,在html文件中引入js文件并加上樣式,使用“$("h1").highlight("highlight")”即可高亮搜索文本。

jquery中什么是高亮顯示

本教程操作系統(tǒng):Windows10系統(tǒng)、jQuery 3.6.4版本、Dell G3電腦。

jQuery的高亮顯示,是指對頁面搜索關(guān)鍵詞時進(jìn)行高亮顯示,其實(shí)現(xiàn)的辦法:

方法一

$(function () {
    //1.獲取要高亮顯示的行
    var rowNode = $('.tiBlock_3NhqL');
    //2.獲取搜索的內(nèi)容
    var searchContent = $(".searchInput_29REY").val();
    //3.遍歷整行內(nèi)容,添加高亮顏色
    rowNode.each(function () {
        var word = $(this).html();
        word = word.replace(searchContent, '<span style="color: #c00;">' + searchContent + '</span>');
        $(this).html(word);
    });
});
登錄后復(fù)制

rowNode是全部搜索結(jié)果,而searchContent是你具體的搜索文本。

注意: 需要引入jQuery

不推薦這種方式,看起來沒什么問題,但是有個Bug:

當(dāng)搜索內(nèi)容為a的時候,使用var word = $(this).html();獲取待查找元素的html代碼后,會出現(xiàn)“把span標(biāo)簽中的a替換成>...”這樣的問題。

我們需要的是僅僅替換文本內(nèi)容,而不是html代碼,所以來看方法二吧。

方法二

這種方法就是使用jquery highlight高亮插件。

去官網(wǎng)下載js文件: jQuery Highlight Plugin | bartaz @ GitHub

下拉到底部的Where to get it?位置下載。

我現(xiàn)在用的是: http://github.com/bartaz/sandbox.js/raw/master/jquery.highlight.js

把這個js文件引用到你的html文件,然后加上樣式:

<style>
.highlight {background-color: #FFFF88; }
</style>
登錄后復(fù)制

接著只需要js像這樣:$("h1").highlight("highlight"); 就可以高亮搜索文本了。

下面是一個完整實(shí)例:

<!DOCTYPE html>
<html>
  <head>
    <title>regex.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <script type="text/javascript" src="../jquery/jquery-1.11/jquery.min.js"></script>
  <script type="text/javascript" src="../jquery/jquery-highlight/jquery-hightlight.js"></script>
  <style>
  .highlight {background-color: #FFFF88; }
  </style>
  <script>
  $(function(){
  $('#search').click(function(){
  var key = $('#key').val();
    if(key != undefined  && key.length > 0) { //注意要判斷key是否為undefined
    var body = $('#body');
    body.removeHighlight();
    body.highlight(key);
    } else {
    alert("請輸入關(guān)鍵字!")
    }
  });
  });
  </script>
  </head>
  <body>
  <div>
  <input id="key" type="text">
  <input id="search" type="button" value="搜索">
  </div>
    <div id="head">
    This is a test head!
    <div>
    input the search key.
    </div>
    <div>
    click "搜索" button.
    </div>
    </div>
    <h1>如下是搜索區(qū)域!</h1>
    <div id="body">
    This is a test body!
    <div>
    This is a test, a test, test, tes, te, t!
    Do you know and listen this test, I think you don't know this test!
    </div>
    <div>
    這是一個簡答的測試,測試.
    <p>你知道或者聽過這個測試嗎,我認(rèn)為你沒有聽過這個測試!</p>
    </div>
    <div>
    This is a test, a test, test, tes, te, t!
    I know't know this test, yes, this test is a not famous test!
    <p>Follow me test, to test the test! I don't know what do you say?</p>
    </div>
    <div>
    <div>
    這是一個簡答的測試,測試.
    我不知道這個測試,是的,這不是一個注明的測試,
    <span>跟隨我,去測試這個測試,我都不知道我在說什么!</span>
    </div>
    </div>
    <div>
    What do you say? test, only a test?
    <p>你們說什么呢?測試,一個測試?</p>
    </div>
    </div>
  </body>
</html>
登錄后復(fù)制

以上就是jquery中什么是高亮顯示的詳細(xì)內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!

最佳 Windows 性能的頂級免費(fèi)優(yōu)化軟件
最佳 Windows 性能的頂級免費(fèi)優(yōu)化軟件

每個人都需要一臺速度更快、更穩(wěn)定的 PC。隨著時間的推移,垃圾文件、舊注冊表數(shù)據(jù)和不必要的后臺進(jìn)程會占用資源并降低性能。幸運(yùn)的是,許多工具可以讓 Windows 保持平穩(wěn)運(yùn)行。

下載
相關(guān)標(biāo)簽:
來源:php中文網(wǎng)
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn
最新問題
開源免費(fèi)商場系統(tǒng)廣告
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號