Home > Web Front-end > JS Tutorial > body text

How to create a search box with dynamic effects using HTML, CSS and jQuery

王林
Release: 2023-10-25 10:28:57
Original
1145 people have browsed it

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.

  1. 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>动态搜索框</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>
Copy after login

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.

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

These CSS styles add appropriate styling to the container, title, input box, and search suggestion list to make it look more professional.

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

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.

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

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.

  1. 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!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template