What is the method for js to highlight keywords
The main content of this article is to explain "what is the method of js highlighting keywords". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what is the method of js highlighting keywords"!
Highlight the CSS style:
five
.highlight {
Background-color: yellow
}
Highlight _ javascript:
Function highlight () {
ClearSelection (); / / clear the last highlighted content first
Var searchText = $.trim ($('# txtKeyWord'). Val ()); / / get the keywords you enter
If (searchText.length > 0) {
Var regExp = new RegExp (searchText,'g'); / / create a regular expression, where g means global. If g is not used, the first one will not continue to look down.
$('# dialogue .dhc') .each (function () / / traverses the area to be queried
{
Var html = $(this) .html ()
Var newHtml = html.replace (regExp, "" + searchText +'); / / replace the keyword found with the highlight attribute
$(this) .html (newHtml); / / Update the article
})
}
/ / prekeyword = searchText
}
Function clearSelection () {
$('# dialogue .dhc') .each (function () / / traversal
{
$(this) .find ('.attributes') .each (function () / / find the elements of all highlight attributes
{
Var thishtml = $(this) .html ()
$(this) [0] .outerHTML = thishtml
/ / $(this) .replaceWith (thishtml); / / remove their attributes
})
})
}
At this point, I believe you have a deeper understanding of "what is the method of highlighting keywords in js". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!