高亮顯示搜尋到的關鍵字-jquery.highlight.js的用法!,jquery.highlight.js

來源:互聯網
上載者:User

高亮顯示搜尋到的關鍵字-jquery.highlight.js的用法!,jquery.highlight.js

閑話少說,直接看需求: 在一篇文章中尋找關鍵字,找到後高亮顯示.

這種需求可以用js實現,可以用jquery實現,殊不知jquery有現成的外掛程式,親,你不用自己實現了,只需要引入jquery-hightlight.js

注意:如下body內容如此複雜只是為了測試,沒有其他目的.

<!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.length > 0) {    var body = $('#body');    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't know this test!    </div>    <div>    這是一個簡答的測試,測試.    <p>你知道或者聽過這個測試嗎,我認為你沒有聽過這個測試!</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>
實現效果如何呢?看如下:

怎麼樣,夠簡單吧!當然他的功能不僅如此,快點網上去搜尋看吧!如下是我參考的資料:

http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html

使用方法如下:

  • 在<head>中引入jquery檔案及外掛程式檔案jquery.highlight.js
    <script type="text/javascript" src="../js/jquery-1.4.2.js"></script>
    <script type="text/javascript" src="../js/jquery.highlight.js"></script>
    <script type="text/javascript">
    function hightlight(str){
    $(".highlight").highlight(str);
    }
    </script>
  • 在頁面Body部分加入從表單傳遞過來的關鍵字<body onload="hightlight('<%=keywords%>');">
  • Css樣式:<style type="text/css">        .highlight{background-color: #FFAEB1;}        </style>
  • 具體介紹如下:
  • 在css中或通過jquery指定高亮地區的顏色:
     .highlight {background-color: #FFFF88; }
    $(".highlight").css({ backgroundColor: "#FFFF88" });
  • 設定需要高亮顯示的詞語或通過事件觸發高亮顯示。例如:
          $("h1").highlight("highlight");
          //高亮顯示所有h1標籤中的“highligh”
          $("body p").unhighlight();
          //取消所有body中段落裡的高亮顯示;
          $("p").highlight("jQuery highlight plugin");
          //高亮段落中的詞條 “jQuery highlight plugin”
          $("p").highlight(["jQuery", "highlight", "plugin"]);
          //高亮段落中的 “jQuery”,“highlight”及“plugin”
          $("p").highlight("Highlight", { caseSensitive: true });
          //高亮段落中的 “Highlight”,區分大小寫。
          $("p").highlight("light", { wordsOnly: true });
          //高亮段落中的 “light”,必須是完整單詞,“highlight”中的“light”不會高亮
          $("p").highlight("highlight", { element: 'em', className: 'error' });
          //高亮段落中的 “hightlight”,並為其指定父標籤為em,class為error
          $("p").highlight("highlight", { element: 'em', className: 'error' });
          //高亮段落中的 “hightlight”,並為其指定父標籤為em,class為error
         $("body p").highlight("jQuery", { element: 'a', className: 'jQueryLink'});
         $("body p a.jQueryLink").attr({ href: 'http://jquery.com' });
        //高亮段落中的 “jQuery”,並為其加上連結。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.