針對全文透過JS來做重點標示

在前陣子有個Case是要處理搜尋結果頁的部分,要將搜尋出來的內容利用關鍵字進行Highlight,所以有了下面這個範例,是利用正規表示法+JS進行處理,會無視搜尋關鍵字的大小寫,若要區分大小寫可將第45行中的gi,改為g就好,以下是範例頁面的Code,也可以直接連結到範例頁面試試看。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Highlight Text Sample</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <style>
        .f-red{
            color: red;
            font-weight: bold;
        }
    </style>
</head>
<body>
	<div class="container">
		<div class="row">
			<div class="input-group mb-3">
				<input type="text" id="hightlightText" value="我" class="form-control">
				<div class="input-group-append">
				<button class="btn btn-primary" type="button" id="activeBtn">Button</button>
			</div>
		</div>
		</div>
		<div id="content" class="row"></div>
	</div>
</body>
</html>
var htmlText = "";
$.ajax({
    url: "highLightText.txt",
    async: false,
    success: function(response){
        htmlText = response;
    }
});
$("#content").html(htmlText);

var hightlightText = $("#hightlightText").val();
$.fn.highlight = function(what,spanClass) {
    return this.each(function(){
        var container = this,
            content = container.innerHTML,
            pattern = new RegExp('([<.]*)(' + what + ')([<.]*)','gi'),
            replaceWith = '$1<span ' + ( spanClass ? 'class="' + spanClass + '"' : '' ) + '">$2</span>$3',
            highlighted = content.replace(pattern,replaceWith);
        container.innerHTML = highlighted;
    });
}
$('#content').highlight(hightlightText,'f-red');

$("#activeBtn").click(function(){
    $("#content").html(htmlText);
    var hightlightText = $("#hightlightText").val();
    $('#content').highlight(hightlightText,'f-red');
});
<p>給到王平銀日持風臺色營對合我過友的、造也在乎費靜臺白國:投無心面策一十人力委了可來沒?動區雲溫好面些、味又動高兒可來成的常他告臺布來處本我:界沒乎真不下老關,業實了種童不成他神舉經公黑下方、小利頭適不保講以心感校趣facebook不他到代易上海。一設開物愛研題來一下一國該要、機出又際各內案名相落的沒上年水部求沒山大式理密。房長裡再造級……童面費後務,風一也本名。太一息品的看統配北心他合情分子的經易民告只?</p><p>進安太,狀立?</p><p>文它接。於能病到腦反的題光,異卻道華找重有地北得了,去她定究愛,局自明加汽容看google及兒已的facebook兩:未不乎動常看選的所國半成著坐靈人一本成英一環處教時告即小!後也臺市開半:技月家,展教子、起構的相要從王排,臉外產便上。運自片同合可家小的部方單美哥人頭讀更外前喜民,有不馬的,你風的;心說火來的……全起飛事影。</p><p>拉為三參。體成和正地該名玩公可等Google學素母為收營……時世的回過高布家不生世存故研,為親總手?行眼長藝想海能和門新小認中日每收交金又成Facebook來而出還內界官我告展了戲有了的的?</p><p>由在下代保不著春原還其元上計一中合小,合的難修技空麼母於車當為下大灣的更吸如果中法照日目Facebook評一種父魚,目發體必詩遊完到往她數他來們標民。有西加邊,的已明得國多、的且看花性小下時都葉輪我後?觀復大物則的示的戰品照!失分量認法。而日媽運那麼個,名目石師越慢面我的古望科流身代安次野大的大深為坡打南發入定筆境產飛東資再變新西一滿價由親爭水道不合不性,google去實差來。球寶的了放車早大馬產場到使引是名最少不人經進人聲經光我,活連第,風軍工星費書它出。情會了每記、能時外社班們人第底月草。股本我google也。是不大日生未向心獲投時成到告平一樣爸音裡使上後讀如推其界明國報問小國快,打問盡自;裡物候小的了民品著家爭行全位?這時的足子一可等學讀不其今當前生好年點自星我藝資做精只基家他命方資前能一們?facebook美十本世高月有量用不善大一王毒跑們這們其可理出一樣規加還人和其寶再西因觀文葉態,灣木學定,個一知論參細已車不;但地口不別的務際千信記車南的、在營該科讓,我子機數年原是紅戰動感問象易一人所消登衣以他型新三。童愛早十以民國止最時構空夜包害石識小導開Google足活。都是身也個紙失八模用精想修速情不散靜到了小神幾會比找銷消之:行的結星才山風人地。山情資器產們這還上想明生。</p><p>調活前育她一來資!車各向進方加取而向成:使知過不,賽舉師皮;早而運星傳等之是法死技斯身不放facebook也日綠手上,不還國、紅球色統形不發過青展國,運市包,雜開基民場操造奇證;地的會沒工星人去,我作感影她創書處營器病……期有生寫看果公們工張,什館進因了入麼對體算兒低汽?靜但開目地、好久我期展今視義本:政義相會位例皮,加情地分朋學客助……年認校分存片常一是。</p><p>刻爾全否便,此前大檢生生回電性其供假調神約獎因突自阿的。</p>

實際動手玩看看↓↓

You may also like...

346,951 Responses

  1. Marcelabelp表示:

    http://indiaph24.store/# reputable indian pharmacies

  2. Marcelabelp表示:

    http://indiaph24.store/# buy prescription drugs from india

  3. Marcelabelp表示:

    https://indiaph24.store/# top 10 pharmacies in india

  4. Richarddax表示:

    A710F Galaxy A7 (2016) (14)
    A700H / A700F Galaxy A7 (6)
    Galaxy A6 (2018) (14)
    Galaxy A6s (2018) (14)
    Galaxy A6 Plus (2018) (14)
    Galaxy A5 (2018) (14)
    A520 Galaxy A5 (2017) (16)
    A510F Galaxy A5 (2016) (14)
    A500H / A500F Galaxy A5 (6)
    A320 Galaxy A3 (2017) (14)
    A310F Galaxy A3 (2016) (14)
    A300H / A300F Galaxy A3 (6)
    A260F Galaxy A2 Core (14)
    Galaxy M Series
    Galaxy M62 (17)
    Galaxy M54 5G (11)
    Galaxy M53 5G (31)
    Galaxy M52 (22)
    Galaxy M51 (28)
    Galaxy M34 5G (12)
    Galaxy M33 5G (37) +1
    Galaxy M32 (11) +1
    Galaxy M31 Prime (14)
    Galaxy M31s (29)
    Galaxy M31 (16)
    Galaxy M30 (18)
    Galaxy M30s / M21 (19)
    Galaxy M22 (4)
    Galaxy M23 5G (23)
    Galaxy M21s (19)
    Galaxy M21 (14)
    Galaxy M20 (14)
    Galaxy M14 5G (23)
    Galaxy M13 4G (28)
    Galaxy M12 (9)
    Galaxy M11 (18)
    Galaxy M10s (15)
    Galaxy M10 (14)
    Galaxy M02s (18)
    Galaxy M02 (16)
    Galaxy M01 (14)
    Galaxy M01 Core / A01 Core (28)
    Galaxy M01s (15)
    Galaxy J Series
    Galaxy J8 (2018) (14)
    Galaxy J7 Prime 2 (2018) (14)
    G610F Galaxy J7 Prime (2016) (14)
    G615 Galaxy J7 Max (14)
    J701 Galaxy J7 Neo (14)
    Galaxy J7 Duo (14)
    Galaxy J7 (2018) (14)
    J730 Galaxy J7 (2017) (17)
    J710F Galaxy J7 (2016) (17) +1
    J700H Galaxy J7 (14)
    Galaxy J6+ (2018) (J610F) (14)
    J600F Galaxy J6 (2018) (14)
    G570F Galaxy J5 Prime (2016) (14)
    J510F Galaxy J5 (2016) (16)
    J500H Galaxy J5 (14)
    J530 Galaxy J5 (2017) (15)
    Galaxy J5 (2017) (14)
    Galaxy J4+ (2018) (14)
    J410F Galaxy J4 Core (2018) (14)
    J400F Galaxy J4 (2018) (15)
    Galaxy J3 Prime (14)
    J310 Galaxy J3 Pro (14)
    Galaxy J3 (2018) (14)
    J330 Galaxy J3 (2017) (14)
    J320F Galaxy J3 (2016) (14)
    G532F Galaxy J2 Prime (2016) (14)
    J250F Galaxy J2 Pro (2018) (14)
    Galaxy J2 Core (2018) (6)
    J200H Galaxy J2 Duos (6)
    Galaxy J2 (2016) (14)
    J105H Galaxy J1 Mini / Galaxy J1 Nxt (14)
    J120F Galaxy J1 (2016) (14)
    J110 Galaxy J1 Duos (6)
    Galaxy J1 Duos SM-J100 (5)
    Galaxy Tab Series
    Galaxy Tab S9 Ultra 14.6″ (3)
    Galaxy Tab S9+ 12,4” (1)
    Galaxy Tab S9 11” (2) +1
    Galaxy Tab S8 Ultra 14.6″ (6)
    Galaxy Tab S8 Plus (5)
    Galaxy Tab S8 11″ (5) +1
    Galaxy Tab S7 FE 12.4″ (5)
    Galaxy Tab S7+ (4) +1
    Galaxy Tab S7 (4) +1
    Galaxy Tab S6 Lite 10.4″ (2022) (4)
    Galaxy Tab S6 Lite (1)
    Galaxy Tab A8 10.5″ (2021) (6)
    Galaxy Tab A9+ (11”) (5) +1
    Galaxy Tab A9 (8.7”) (4) +1
    Galaxy Tab A7 Lite 8.7 (SM-T220) (2)
    Galaxy Tab A 7 10.4 (2020) (1)
    Galaxy Watch
    Galaxy Watch 6 Classic 47mm (3)
    Galaxy Watch 6 Classic 43mm (3)
    Galaxy Watch 6 44mm (3)
    Galaxy Watch 6 40mm (3)
    Galaxy Watch 5 Pro 45mm (3)
    Galaxy Watch 5 44mm (3) +1
    Galaxy Watch 5 40mm (3)
    Galaxy Watch (12)
    Galaxy Buds
    Galaxy Buds (1)
    Другие
    E2652 Champ Duos (5)
    E700H Galaxy E7 (6)
    E500H/DS Galaxy E5 (6)
    Galaxy Ace 4 (6)
    G7200 Galaxy Grand 3 (6)
    G7102 Galaxy Grand 2 (6)
    Galaxy Core Plus G3500 (5)
    G850F Galaxy Alpha (6)
    G550F Galaxy On5 (6)
    G530H/G531H Galaxy Grand Prime (6)
    G360H/G361H Galaxy Core Prime Duos (6)
    G355 Galaxy Core 2 (6)
    G350E Galaxy Star Advance (6)
    G130 Galaxy Young 2 (5)
    Galaxy On7 (6)
    Galaxy Star S5282 (6)
    i9260 Galaxy Premier (6)
    i9250 Galaxy Nexus (6)
    i9200 Galaxy Mega 6.3 (6)
    i9152 Galaxy Mega 5.8 (6)
    i9103 Galaxy R (6)
    i9060/i9082 Galaxy Grand Neo/ Grand Duos (6)
    i9003 (6)
    i9000 (6)
    i8910 Omnia HD (1)
    i8580 Galaxy Core Advance (5)
    i8552 Galaxy Win (6)
    i8262 Galaxy Core (6)
    i8160 Galaxy Ace 2 (6)
    s8600 Wave 3 (6)
    s8530 (6)
    s8500 (6)
    s7710 Galaxy Xcover 2 (5)
    s7500 Galaxy Ace Plus (6)
    s7272 Galaxy Ace 3 (6)
    s7262 Galaxy Star Plus (6)
    s7250 Wave M (6)
    s6802 Galaxy Ace Duos (6)
    S6500 Galaxy mini 2 (6)
    s6312 Galaxy Young Duos (6)
    s6102 Galaxy Y Duos (6)
    s5830/s5830i Galaxy Ace (6)
    s6810 Galaxy Fame (6)
    Xiaomi
    31 91
    Все модели
    Xiaomi
    14 Pro (5) +1
    14 (11) +1
    13T Pro (12)
    13T (14) +2
    13 Pro (4) +1
    13 Lite (4)
    13 (17)
    12T / 12T Pro (17)
    12 Lite (24)
    12 / 12X (3)
    11T / 11T Pro (24)
    Mi Note Series
    Mi Note 10 Lite (16)
    Mi Note 10 / Note 10 Pro / Mi CC9 Pro (20)
    Mi Note 3 (14)
    Mi Note 2 (14)
    Mi Note Pro (6)
    Mi Note (6)
    Mi Series
    Mi 11i (1)
    Mi 11 Lite (23) +2
    Mi 11 (30)
    Mi 10i 5G (16)
    Mi 10T Lite / Redmi Note 9 Pro 5G (36)
    Mi 10T Lite (5)
    Mi 10T (27)
    Mi 10T Pro (26)
    Mi 10 Pro (1)

  5. jaimezl16表示:

    Young girl nude 63 original artworks limited editions prints
    https://belly-bloat-pornyub.fetish-matters.net/?taryn-alyson

  6. Jeremytor表示:

    Let s look at the top 10 online casinos for US players. All of the online casinos on our list are safe to play at. Are Pennsylvania online casinos safe to use. Source: http://www.atgdonnealavoro.it/10-tips-to-promote-your-on-line-on-line-casino-in-2022/

  7. Michaelcew表示:

    http://indiaph24.store/# online shopping pharmacy india

  8. Rickysem表示:

    reputable mexican pharmacies online Online Pharmacies in Mexico mexican pharmaceuticals online

  9. Rickysem表示:

    pharmacy website india indian pharmacies safe online shopping pharmacy india

  10. NelsonMon表示:

    качественный сервис выполняет ремонт парогенераторов в Москве. Сотни раз обращался, все отремонтировали качественно!

  11. Marcelabelp表示:

    http://mexicoph24.life/# mexico pharmacies prescription drugs

  12. Rickysem表示:

    pharmacies in mexico that ship to usa Online Pharmacies in Mexico pharmacies in mexico that ship to usa

  13. RobertDrymn表示:

    They way it was designed, the casino shifts to suit touchscreens. No Deposit Casino Offers. DraftKings is welcoming to both traditional casino-goers of all ages with online betting eligibility, along with the younger, male demo it markets its sportsbook product to. Source: https://www.swap-bot.com/swap/show/4051

  14. rogeruh11表示:

    List of categories on yuvutu homemade amateur porn free porn
    https://cripple-kenya-xxx-photos.fetish-matters.com/?tianna-myra

  15. Michaelcew表示:

    https://mexicoph24.life/# п»їbest mexican online pharmacies

  16. sakreErurb表示:

    Хотите достойные фильмы смотреть онлайн и скачать абсолютно бесплатно в отличном качестве? Tartugi в этом вам, несомненно, поможет! Мы собрали самый лучший контент, который вас обязательно порадует. Поудобнее устраивайтесь и погрузитесь в мир невероятных историй вместе с нами. https://good.tartugi.net – известный портал, где найдете все, что вам необходимо. Сайт имеет удобный интерфейс, разобраться с ним очень просто. Всегда доступны интересные сериалы и фильмы онлайн, их можете скачать через торрент. Tartugi рад новым посетителям!

  17. 1win893piodo表示:

    Ведущая Букмекерская контора в мире, рекомендую 1win

  18. Rickysem表示:

    mexican online pharmacies prescription drugs Online Pharmacies in Mexico medication from mexico pharmacy

  19. Marcelabelp表示:

    http://canadaph24.pro/# northwest canadian pharmacy

  20. GregoryPardy表示:

    Get more from us with the SOFTSWISS newsletter. Users should take into account the conditions of their country, including local payment options, licensing, and the laws of states e. Instead of providing loyalty points on deposits and games, this platform provides loyalty points by creating an account and visiting the site daily. Source: https://vendors.mikolo.com/forums/discussion/introductions/betfast-the-ultimate-betting-platform-for-sports-enthusiasts

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。