This article shares the specific code of JavaScript to display hidden form text for your reference. The specific content is as follows Implementation ideasUse onfocus and onblur events onfocus- - -Get the focus (mouse clicks on the input box, and there is a flashing cursor in the input box) onblur- - - Loss of focus (the mouse is not selected in the input box, and the flashing cursor in the input box is lost) 1. Set a default value for the input box 2. Get the input box object and bind events to it: onfocus and onblur 3. You can also set different text colors for the focus and focus-lost states to make the two states more distinct. Example 1:Code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Get/Lose Focus</title> <style> input { color: #ccc; outline: none; } </style> </head> <body> <input type="text" value="Mobile"> <script> var text = document.querySelector('input'); text.onfocus = function() { if (this.value === 'mobile phone') { this.value = ''; } this.style.color = '#333'; } text.onblur = function() { if (this.value === '') { this.value = 'Mobile phone'; } this.style.color = '#ccc'; } </script> </body> </html> Page effect: Example 2Code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Username display hide</title> <style> input { font-size: 14px; color: #999; outline: none; padding: 3px 0 3px 10px; border: 1px solid #ccc; } </style> </head> <body> <!--Username shows hidden content--> <input type="text" value="Email/ID/Phone Number" class="userName"> <script> var userName = document.querySelector('.userName'); userName.onfocus = function() { if (this.value === 'email/ID/phone number') { this.value = ''; this.style.borderColor = 'pink'; } else { this.style.borderColor = 'pink'; this.style.color = '#999'; } } userName.onblur = function() { if (this.value === '') { this.value = 'Email/ID/Phone Number'; this.style.borderColor = '#ccc'; this.style.color = '#999'; } else { this.style.borderColor = '#ccc'; this.style.color = '#333'; } } </script> </body> </html> Page effect: The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: How to monitor Tomcat using LambdaProbe
>>: Analysis of the process of building a cluster environment with Apache and Tomcat
need: Implement dynamic display of option values ...
Table of contents Preface System environment Curr...
Jupyter notebook is configured under the docker c...
There are three ways to introduce CSS: inline sty...
question: In some browsers, such as 360 browser...
This article uses examples to explain the concept...
Detailed explanation of MySQL stored procedures, ...
for loop The for loop loops through the elements ...
First look at the effect diagram: The complete co...
Table of contents 1. Vue2 syntax 2. Use of Vue3 1...
Some people say that IE9 is Microsoft's secon...
For more information about operating elements, pl...
1. Introduction to Nginx Nginx is a web server th...
Since the default Linux kernel parameters are bas...
Effect If you use it, please optimize the code an...