Highlight the keywords found-jquery. highlight. js usage !, Jquery. highlight. js

Source: Internet
Author: User

Highlight the keywords found-jquery. highlight. js usage !, Jquery. highlight. js

Let's talk less and look at the demand directly: Search for keywords in an article, find them, and highlight them.

This requirement can be achieved with js, can be achieved with jquery, but do not know that jquery has ready-made plug-ins, pro, you do not need to implement your own, just need to introduce jquery-hightlight.js

Note: The following body content is so complicated only for testing and has no other purpose.

<! DOCTYPE html> What is the result? See the following:


How are you doing? It's easy enough! Of course, this is not just about his function. Hurry up and search for it online! The following are my references:

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

The usage is as follows:

  • Introduce the jquery file and plug-in file jquery. highlight. js in
    <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>
  • Add the keyword <Body onload = "hightlight ('<% = keywords %>');">
  • Css style: <style type = "text/css">. highlight {background-color: # FFAEB1 ;}</style>
  • The details are as follows:
  • Specify the color of the highlighted area in css or jquery:
     .highlight {background-color: #FFFF88; }
    $(".highlight").css({ backgroundColor: "#FFFF88" });
  • Set the words to be highlighted or use events to trigger the highlighted display. For example:
    $ ("H1"). highlight ("highlight ");
    // Highlight "highligh" in all h1 labels"
    $ ("Body p"). unhighlight ();
    // Deselect the highlighted section in all bodies;
    $ ("P"). highlight ("jQuery highlight plugin ");
    // The entry "jQuery highlight plugin" in the highlighted section"
    $ ("P"). highlight (["jQuery", "highlight", "plugin"]);
    // "JQuery", "highlight", and "plugin" in the highlighted section"
    $ ("P"). highlight ("Highlight", {caseSensitive: true });
    // Highlight "Highlight" in a section, case sensitive.
    $ ("P"). highlight ("light", {wordsOnly: true });
    // The "light" in the highlighted section must be a complete word. The "light" in the "highlight" section will not be highlighted.
    $ ("P"). highlight ("highlight", {element: 'em ', className: 'error '});
    // Highlight "hightlight" in the Section and specify the parent label as em and class as error.
    $ ("P"). highlight ("highlight", {element: 'em ', className: 'error '});
    // Highlight "hightlight" in the Section and specify the parent label as em and class as error.
    $ ("Body p"). highlight ("jQuery", {element: 'A', className: 'jquerylink '});
    $ ("Body p a. jQueryLink"). attr ({href: 'http: // jquery.com '});
    // Highlight "jQuery" in the Section and add a link to it.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.