Home  >  Article  >  Web Front-end  >  How to optimize the HTML input box

How to optimize the HTML input box

php中世界最好的语言
php中世界最好的语言Original
2018-02-03 09:33:501688browse

This time I will show you how to optimize the HTML input box. What are the precautions for optimizing the HTML input box? The following is a practical case, let's take a look.

In order to improve user experience and ease of use, some designers will optimize things that users often 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>

The most important part of this code is the onfocus part. If you don’t use onfocus, use

onclickThe same effect can also be achieved, such as onfocus="this.select()".
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 using A short piece of javascript; The HTML code of method 1 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 input box border will become Red, but this method is only valid in Firefox browser and IE7 or above. IE6 does not support it, so it has certain limitations. The code of method two is mostly the same as the above example, except that a 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=&#39;#FF6600&#39;" onmouseout="this.style.borderColor=&#39;&#39;" /> 
</form>

Using this code, most browsers can support it.


3. Click the input box and the default text disappears

There is another effect. 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 adding a short piece of javascript judgment:

<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=&#39;#FF6600&#39;" onmouseout="this.style.borderColor=&#39;&#39;" onFocus="if (value ==&#39;Dreamweaver&#39;){value =&#39;&#39;}" onBlur="if (value ==&#39;&#39;){value=&#39;Dreamweaver&#39;}"/> 
</form>

You can directly use the placeholder attribute of input in HTML5:


<input type="search" name="user_search" placeholder="Search W3School" />

The above three effects are relatively simple. Although JavaScript applications have gone beyond the scope of Html code, mastering them brings great convenience to Html applications and web page production. Therefore, it is necessary to master some simple JavaScript when necessary.

I believe you have mastered the methods after reading these cases. For more exciting information, please pay attention to other related articles on the php Chinese website!

Related reading:

How to solve the problem when the form automatically submits and jumps to other pages after pressing Enter on the webpage

How The width of the table can be fixed table-layout: fixed

The above is the detailed content of How to optimize the HTML input box. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:Textarea tag settingsNext article:Textarea tag settings