首頁  >  文章  >  jquery中什麼是高亮顯示

jquery中什麼是高亮顯示

百草
百草原創
2023-06-13 10:12:511519瀏覽

jQuery中高亮顯示是指對頁面搜尋關鍵字時進行高亮顯示,其實現方法:1、先取得要高亮顯示的行,取得搜尋的內容,再遍歷整行內容,最後在添加高亮顏色;2、使用“jQuery highlight”高亮插件,官網下載js檔案後,在html檔案中引入js檔案並加上樣式,使用“$("h1").highlight("highlight")”即可高亮搜尋文字。

jquery中什麼是高亮顯示

本教學作業系統:Windows10系統、jQuery 3.6.4版本、Dell G3電腦。

jQuery的高亮顯示,是指對頁面搜尋關鍵字時進行高亮顯示,其實現的方法:

方法一

$(function () {
    //1.获取要高亮显示的行
    var rowNode = $('.tiBlock_3NhqL');
    //2.获取搜索的内容
    var searchContent = $(".searchInput_29REY").val();
    //3.遍历整行内容,添加高亮颜色
    rowNode.each(function () {
        var word = $(this).html();
        word = word.replace(searchContent, &#39;<span style="color: #c00;">&#39; + searchContent + &#39;</span>&#39;);
        $(this).html(word);
    });
});

rowNode是全部搜尋結果,searchContent是你具體的搜尋文字。

注意: 需要引入jQuery

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

當搜尋內容為a的時候,使用var word = $(this).html();取得待查找元素的html程式碼後,會出現「把span標籤中的a替換成b76781af6de7e0352d0f8e80d2f2bbc8 >...45a2772a6b6107b401db3c9b82c049c2」這樣的問題。

我們需要的是僅僅替換文字內容,而不是html程式碼,所以來看方法二吧。

方法二

這種方法就是使用jquery highlight高亮外掛。

去官網下載js檔案: jQuery Highlight Plugin | bartaz @ GitHub

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

我現在用的是:http://github.com/bartaz/sandbox.js/raw/master/jquery.highlight.js

##把這個js檔案引用到你的html文件,然後加上樣式:


<style>
.highlight {background-color: #FFFF88; }
</style>

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

下面是一個完整實例:


<!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(){
  $(&#39;#search&#39;).click(function(){
  var key = $(&#39;#key&#39;).val();
    if(key != undefined  && key.length > 0) { //注意要判断key是否为undefined
    var body = $(&#39;#body&#39;);
    body.removeHighlight();
    body.highlight(key);
    } else {
    alert("请输入关键字!")
    }
  });
  });
  </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>如下是搜索区域!</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&#39;t know this test!
    </div>
    <div>
    这是一个简答的测试,测试.
    <p>你知道或者听过这个测试吗,我认为你没有听过这个测试!</p>
    </div>
    <div>
    This is a test, a test, test, tes, te, t!
    I know&#39;t know this test, yes, this test is a not famous test!
    <p>Follow me test, to test the test! I don&#39;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>

以上是jquery中什麼是高亮顯示的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn