How to implement text highlighting in jQuery?
jQuery is a popular JavaScript library used to simplify the manipulation and event handling of HTML documents. When implementing the text highlighting function, you can use jQuery through the following steps:
- Introduce the jQuery library file:
First, you need to introduce the jQuery library file into the HTML file. You can Add to the page via CDN link or local file. Add the following code snippet in the tag:
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
- Writing HTML structure:
Add a text box or other HTML elements to the page for input that needs to be highlighted The text content displayed. The code example is as follows:
<input type="text" id="searchInput" placeholder="输入需要高亮显示的文本"> <button id="highlightBtn">高亮显示</button> <div id="content"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam vehicula metus ac odio. </div>
- Write jQuery code:
Next, write code through jQuery to implement the text highlighting function. The code example is as follows:
$(document).ready(function() { $("#highlightBtn").click(function() { var searchString = $("#searchInput").val(); var content = $("#content").text(); var highlightedContent = content.replace(new RegExp(searchString, 'gi'), function(match) { return '<span class="highlighted">' + match + '</span>'; }); $("#content").html(highlightedContent); }); });
In the above code, first get the search text in the input box and the text in the content area. Then use JavaScript's replace method combined with regular expressions to add a tag to the matched text to achieve highlighting. Finally, the processed content is reset to the content area.
- CSS style setting:
In order to make the highlighting effect better, you need to set the style of the highlighted text in the CSS file. The code example is as follows:
.highlighted { background-color: yellow; font-weight: bold; }
Through the above steps, you can realize the text highlighting function on the page. After the user enters the text that needs to be highlighted, click the button to highlight the matching text content. The power and convenience of jQuery make it easy and efficient to implement text highlighting.
The above is the detailed content of How to implement text highlighting in jQuery?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to read excel data in html: 1. Use JavaScript library to read Excel data; 2. Use server-side programming language to read Excel data.

We will also cover another way to execute a PHP function through the onclick() event using the Jquery library. This method calls a javascript function, which will output the content of the php function in the web page. We will also demonstrate another way to execute a PHP function using the onclick() event, calling the PHP function using pure JavaScript. This article will introduce a way to execute a PHP function, use the GET method to send the data in the URL, and use the isset() function to check the GET data. This method calls a PHP function if the data is set and the function is executed. Using jQuery to execute a PHP function through the onclick() event we can use

Use the <br> tag in Dreamweaver to create line breaks, which can be inserted through the menu, shortcut keys or direct typing. Can be combined with CSS styles to create empty rows of specific heights. In some cases, it is more appropriate to use the <p> tag instead of the <br> tag because it automatically creates blank lines between paragraphs and applies style control.

Detailed explanation of how to replace newlines in PHP In PHP development, sometimes we need to replace or process newlines in strings. Line breaks may be expressed differently on different platforms, so they need to be processed uniformly to ensure that strings display consistently in different environments. This article will introduce in detail how to replace newlines in PHP, including common newline characters and specific code examples. 1. Common newline characters In different operating systems, the representation of newline characters may be slightly different. The main newline characters include: Windo

Title: PHP replacement skills revealed! PHP, as a popular server-side scripting language, is often used to handle data replacement and manipulation. In daily development, we often encounter situations where strings need to be replaced. This article will reveal some commonly used replacement techniques in PHP and give specific code examples to help readers better master these techniques. 1. Use the str_replace() function The str_replace() function is one of the most commonly used string replacement functions in PHP. it can

How to find other people with Momo ID? In Momo APP, you can find users by ID, but most users don’t know how to query and add Momo ID. Next is the picture of how to find other people with Momo ID brought by the editor. Text tutorial, interested users come and take a look! Momo usage tutorial: How to find others with Momo ID 1. First open Momo APP and enter the main page, click the [Message] function at the bottom; 2. Then challenge to the message function page, click the [Portrait] icon in the upper right corner; 3. Then On the friend page, click the button in the upper right corner as shown by the arrow in the figure below; 4. Then challenge to the added interface, enter the ID account in the border to find others.

Ridge is a border style in CSS that is used to create a 3D border with an embossed effect, which is manifested as a raised ridge-like line.

Can PHP replace the functionality of JSP? As web development technology continues to evolve, developers are often faced with choosing the appropriate server-side language to implement their project needs. In this regard, PHP and JSP are two common choices. JSP is the abbreviation of JavaServerPages, which is a server-side technology based on Java, while PHP is a server-side scripting language. This article will explore whether PHP can replace the functions of JSP and provide some specific code examples to help readers better understand
