Home Web Front-end JS Tutorial How to implement text highlighting in jQuery?

How to implement text highlighting in jQuery?

Feb 27, 2024 pm 12:12 PM
Find replace html element Highlight

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:

  1. 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>
Copy after login
  1. 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>
Copy after login
  1. 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);
    });
});
Copy after login

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.

  1. 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;
}
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to read excel data in html How to read excel data in html Mar 27, 2024 pm 05:11 PM

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.

Execute PHP function using onclick Execute PHP function using onclick Feb 29, 2024 pm 04:31 PM

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

What is dreamweaver line break? What is dreamweaver line break? Apr 08, 2024 pm 09:54 PM

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 Detailed explanation of how to replace newlines in PHP Mar 20, 2024 pm 01:21 PM

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

PHP replacement skills revealed! PHP replacement skills revealed! Mar 28, 2024 am 08:57 AM

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 on Momo ID How to find other people on Momo ID Mar 02, 2024 pm 03:40 PM

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.

What does ridge mean in css What does ridge mean in css Apr 28, 2024 pm 04:06 PM

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? Can PHP replace the functionality of JSP? Mar 20, 2024 pm 03:45 PM

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

See all articles