


How to create a search box with dynamic effects using HTML, CSS and jQuery
How to use HTML, CSS and jQuery to create a search box with dynamic effects
In modern web development, a common need is to create a search box with dynamic effects Effect search box. This search box can display search suggestions in real time and automatically complete keywords as the user types. This article will introduce in detail how to use HTML, CSS and jQuery to implement such a search box.
- Create HTML structure
First, we need to create a basic HTML structure. The code is as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>动态搜索框</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="container"> <h1 id="动态搜索框">动态搜索框</h1> <input type="text" id="search-input" placeholder="请输入关键词"> <ul id="suggestion-list"></ul> </div> <script src="jquery.min.js"></script> <script src="script.js"></script> </body> </html>
This HTML structure contains a container div, which includes a title and an input box for search. We also created a ul element to display search suggestions.
- Write CSS styles
Next, we need to write CSS styles to add some basic styles to the search box to make it look more beautiful. The code is as follows:
.container { max-width: 500px; margin: 0 auto; padding: 20px; text-align: center; } h1 { font-size: 24px; } input[type="text"] { width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 5px; margin-bottom: 10px; } #suggestion-list { text-align: left; list-style-type: none; padding: 0; display: none; } #suggestion-list li { padding: 10px; background-color: #f2f2f2; cursor: pointer; } #suggestion-list li:hover { background-color: #e3e3e3; }
These CSS styles add appropriate styling to the container, title, input box, and search suggestion list to make it look more professional.
- Add the function of search suggestions
Now we need to use jQuery to implement the function of search suggestions. The code is as follows:
$(document).ready(function() { $('#search-input').keyup(function() { var keyword = $(this).val(); if (keyword !== '') { $.ajax({ url: 'suggestion.php', type: 'POST', data: { keyword: keyword }, success: function(data) { if (data.trim() !== '') { $('#suggestion-list').html(data).show(); } else { $('#suggestion-list').html('').hide(); } } }); } else { $('#suggestion-list').html('').hide(); } }); $(document).on('click', '#suggestion-list li', function() { var suggestion = $(this).text(); $('#search-input').val(suggestion); $('#suggestion-list').html('').hide(); }); });
This code first listens to the keyboard event of the input box. When the user inputs, it uses AJAX to send a request to a back-end file named suggestion.php and passes the keywords entered by the user. to the backend. The backend will return corresponding search suggestions, and then the frontend will dynamically update the search suggestion list based on the returned data.
When the user clicks on an item in the search suggestion list, the content of the item will be filled in the input box and the search suggestion list will be hidden.
- Write the back-end code (suggestion.php)
For the back-end code, we can use PHP or other server-side languages to implement it. Here we take PHP as an example to show a simple sample code:
<?php $keyword = $_POST['keyword']; // 从数据库或其他数据源获取搜索建议 $suggestions = array( '苹果', '香蕉', '橙子', '草莓', '葡萄', '西瓜', '梨', '柚子' ); $output = ''; foreach ($suggestions as $s) { if (strpos($s, $keyword) !== false) { $output .= '<li>' . $s . '</li>'; } } echo $output; ?>
This code receives the keywords passed by the front end and obtains matching search suggestions from the database or other data sources based on the keywords. Then the suggestions are spliced into HTML list items in the required format and returned to the front end.
- Run
After saving the above code as the corresponding file and importing the corresponding CSS and JavaScript libraries, open the browser and run the HTML file, you can see a band Search box with dynamic effects.
Summary
By using HTML, CSS and jQuery, we can easily create a search box with dynamic effects. By monitoring user input, sending AJAX requests to the backend to obtain search suggestions, and dynamically updating the search suggestion list, users can easily select search suggestions or directly enter keywords to search. This search box can be used in the search functions of various websites to provide users with a better experience.
The above is the detailed content of How to create a search box with dynamic effects using HTML, CSS and 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



Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text
