When we are writing Web pages inevitably encounter input boxes, then how to design the input box to be more elegant? Different people will have different answers, below to share a relatively good design.
Details
This effect is mainly achieved through jquery, my thinking is as follows:
Before the input box gets the mouse focus, the value of the original tag is displayed, and after the mouse focus is obtained, if the current value has values, then it will be emptied or restored; Password box special care, will speak later.
The implementation code is as follows:
?
123456789101112 |
$(
"#address"
).focus(
function
(){
var address_text = $(
this
).val();
if
(address_text==
"请输入邮箱地址"
){
$(
this
).val(
""
);
}
});
$(
"#address"
).blur(
function
(){
var address_value = $(
this
).val();
if
(address_value==
""
){
$(
this
).val(
"请输入邮箱地址"
)
}
});
|
A complete small example
The complete code is as follows, paying particular attention to the implementation of <input type= "text" id= "password" > !
?
123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
<!DOCTYPE html>
<
html
>
<
head
>
<
meta charset
=
"utf-8"
>
<
title
>文本输入框中内容的提示效果</
title
>
<
script type
=
"text/javascript" src
=
"jquery-2.2.4.min.js"
></
script
>
</
head
>
<
body
>
<
script
>
$(function(){
$("#address").focus(function(){
var address_text = $(this).val();
if(address_text=="请输入邮箱地址"){
$(this).val("");
}
});
$("#password").focus(function(){
var password_text = $(this).val();
if(password_text=="请输入密码"){
$(this).attr("type","password");
$(this).val("");
}
});
$("#address").blur(function(){
var address_value = $(this).val();
if(address_value==""){
$(this).val("请输入邮箱地址")
}
});
$("#password").blur(function(){
var password_value = $(this).val();
if(password_value==""){
$(this).attr("type","text");
$(this).val("请输入密码")
}
});
});
</
script
>
<
div align
=
"center"
>
<
input type
=
"text" id =
"address" value
=
"请输入邮箱地址"
><
br
><
br
>
<
input type
=
"text" id =
"password" value
=
"请输入密码"
><
br
><
br
>
<
input type
=
"button" name
=
"登录" value
=
"登陆"
>
</
div
>
</
body
>
</
html
>
|
$ (function () {}); It is the abbreviation for $ (document). Ready (function () {}); That's not a big point.
$ (this). attr ("type", "password"); Changes the property value of the current object (that is, the input box that gets the mouse focus) dynamically. When the input data is reached, it appears in the form of a password box.
The input box prompt statement for jquery practical tips