Home Web Front-end JS Tutorial Hide/display text when the input input box gains/loses focus (jquery version)_jquery

Hide/display text when the input input box gains/loses focus (jquery version)_jquery

May 16, 2016 pm 05:38 PM
input focus Input box

The input input box hides or displays text when it gains and loses focus. Let’s take a look at the rendering first.
The default state of the input box:
Hide/display text when the input input box gains/loses focus (jquery version)_jquery
The input box gets focus state:
Hide/display text when the input input box gains/loses focus (jquery version)_jquery
You can see the search input box in the rendering. The prompt "Username/Email" is displayed by default. When the input box gains focus, it will automatically clear and wait for user input. When the user enters nothing, he will leave the input box. , the prompt “Username/Email” is displayed again in the input box. Is it very common? Many searches, logins, and forms will use this effect, but I have looked at many websites, and more than 90% of them are implemented this way:

Copy code The code is as follows:



I am very opposed to writing javascript in html In the tag, this is the same as writing style in the html tag. Although it does not violate W3C standards, it is not recommended to write it this way. Because:
1. There is no reusability at all. If it is a form with many input boxes, and each one needs such an effect, should each one be treated in this way?
2. If you want to modify the prompt text, it is time-consuming, laborious and difficult to maintain.
3. We advocate the separation of structure (html), performance (css), and behavior (javascript). This is a good page.
So how do you write to achieve this effect, which is reusable and easy to maintain without writing js into html?
The specific method is as follows:
First of all, you must introduce jQuery
Html code:
Copy the code The code is as follows:




jQuery code:
Copy code The code is as follows:




You can easily implement it by adding the class "input_test" to the implemented input box
View: Demo
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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 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 encapsulate input components and unified form data in vue3 How to encapsulate input components and unified form data in vue3 May 12, 2023 pm 03:58 PM

Preparation Use vuecreateexample to create a project. The parameters are roughly as follows: use native input. Native input is mainly value and change. The data needs to be synchronized when changing. App.tsx is as follows: import{ref}from'vue';exportdefault{setup(){//username is the data constusername=ref('Zhang San');//When the input box changes, synchronize the data constonInput=;return( )=>({

How to implement laravel input hidden field How to implement laravel input hidden field Dec 12, 2022 am 10:07 AM

How to implement the laravel input hidden field: 1. Find and open the Blade template file; 2. Use the method_field method in the Blade template to create a hidden field. The creation syntax is "{{ method_field('DELETE') }}".

C/C++ program to find the vertex, focus and directrix of a parabola C/C++ program to find the vertex, focus and directrix of a parabola Sep 05, 2023 pm 05:21 PM

Asetofpointsonaplainsurfacethatformsacurvesuchthatanypointonthatcurveisequidistantfromapointinthecenter(calledfocus)isaparabola.Thegeneralequationfortheparabolaisy=ax2+bx+cThevertexofaparabolaisthecoordinatefromwhichittakesthesharpestturnwhereasaisth

How to remove focus in jquery How to remove focus in jquery Feb 17, 2023 am 10:20 AM

How to remove focus in jquery: 1. Get the focus through "document.getElementById('test').focus()"; 2. Use the "document.getElementById('test').blur();" method to remove the focus .

What to do if there is no cursor when clicking on the input box What to do if there is no cursor when clicking on the input box Nov 24, 2023 am 09:44 AM

Solutions for clicking the input box without a cursor: 1. Confirm the focus of the input box; 2. Clear the browser cache; 3. Update the browser; 4. Use JavaScript; 5. Check the hardware device; 6. Check the input box properties; 7. Debug JavaScript code; 8. Check other elements of the page; 9. Consider browser compatibility.

Detailed explanation of input box binding events in Vue documents Detailed explanation of input box binding events in Vue documents Jun 21, 2023 am 08:12 AM

Vue.js is a lightweight JavaScript framework that is easy to use, efficient and flexible. It is one of the most popular front-end frameworks currently. In Vue.js, input box binding events are a very common requirement. This article will introduce the input box binding events in the Vue document in detail. 1. Basic concepts In Vue.js, the input box binding event refers to binding the value of the input box to the data object of the Vue instance, thereby achieving two-way binding of input and response. In Vue.j

Use jQuery to implement an input box that only allows numbers and decimal points to be entered Use jQuery to implement an input box that only allows numbers and decimal points to be entered Feb 26, 2024 am 11:21 AM

Implement jQuery input box to limit the input of numbers and decimal points. In web development, we often encounter the need to control what users input in the input box, such as restricting the input of numbers and decimal points only. This restriction can be achieved through JavaScript and jQuery. The following will introduce how to use jQuery to implement the function of limiting the input of numbers and decimal points in the input box. 1. HTML structure First, we need to create an input box in HTML, the code is as follows:

How to use the input box carriage return event and verification function in the Vue document How to use the input box carriage return event and verification function in the Vue document Jun 20, 2023 am 09:13 AM

Vue is a popular JavaScript front-end framework with a responsive data binding and component system at its core. In Vue applications, the input box is one of the most commonly used UI elements. When the user enters text, we hope to listen for the carriage return event and validate the input before submitting. This article will introduce the input box enter event and verification function usage in the Vue document. 1. The carriage return event of the input box in Vue. Monitoring the carriage return event of the input box in Vue is very simple.

See all articles