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.