Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method for js to highlight keywords

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report