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
This article shares with you the graphic tutorial...
Table of contents Preface What is VirtualDOM? Rea...
1. Installation of the decompressed version (1). ...
Table of contents Preface Optional Chaining Nulli...
What is text wrapping around images? This is the ...
Tag type (display mode) HTML tags are generally d...
What are the attributes of the JS script tag: cha...
Problem Description A Spring + Angular project wi...
Table of contents Written in front Login Overview...
1. Overview The so-called life cycle function is ...
1. Introduction to yum Yum (full name Yellow dogU...
This article example shares the specific code of ...
When the database concurrently adds, deletes, and...
MySQL 5.5 installation and configuration method g...
describe This article introduces a method to impl...