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

Js creates the effect of the default text disappearing when the input box is clicked_Text special effects

WBOY
Release: 2016-05-16 15:40:46
Original
1339 people have browsed it

In order to improve user experience and ease of use, some designers will optimize things that users frequently use on web pages, such as input boxes. How are general input boxes optimized? From the perspective of user experience, simplifying the user steps and making it more convenient for users improves usability. For example, changing the color of the input box when the mouse is hovering over the input box, automatically selecting the default text in the input box, or clicking to enter Automatically clear the default content when the box is opened, etc. This effect sounds complicated, but it is actually very simple to do. It only takes a short piece of JavaScript code to solve it. The following introduces the codes for several effects:

1. Click the input box to select the Html code of the content:

<form id="form1" name="form1" method="post" action=""> 
<label for="textfield">输入内容:</label> 
<input name="textfield" type="text" id="textfield" value="Dreamweaver"onfocus="this.select()" /> 
</form>
Copy after login

2. Change the border color or background color when the mouse is hovering over the input box

There are two methods for this effect: method one is to use the pseudo element in CSS: focus; method two is still to use a small piece of javascript; method one’s html code is the same as the above example, except that the following paragraph is added to the CSS :
input:hover { border:1px solid #F00; }
When the mouse is hovering over the input box, the border of the input box will turn red. However, this method is only valid in Firefox browser and IE7 or above. IE6 does not support it, so it has certain limitations. The code for method two is mostly the same as the above example, except that a section of mouse hover code is added at the end:

<form id="form1" name="form1" method="post" action=""> 
<label for="textfield">输入内容:</label> 
<input name="textfield" type="text" id="textfield" value="Dreamweaver"onfocus="this.select()"onmouseover="this.style.borderColor='#FF6600'" onmouseout="this.style.borderColor=''" /> 
</form>
Copy after login

Using this code, most browsers can support it.
3. Click the input box and the default text will disappear
There is also an effect that when the mouse clicks on the input box, the original default text disappears. If you enter other new content and then move the mouse away, the new content of the input box remains unchanged; if you do not enter new content, the mouse leaves the input box and the default text is restored. This effect can be achieved by just adding a short section of javascript:

<form id="form1" name="form1" method="post" action="">
<label for="textfield">输入内容:</label> 
<input name="textfield" type="text" id="textfield" value="Dreamweaver" onmouseover="this.style.borderColor='#FF6600'" onmouseout="this.style.borderColor=''" onFocus="if (value =='Dreamweaver'){value =''}" onBlur="if (value ==''){value='Dreamweaver'}"/> 
</form>
Copy after login

The above three effects are relatively simple javascript applications.

Related labels:
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