需求:
代码:
<
script
type
="text/javascript"
>
$( function () {
$( " input " ).val( " 请输入关键字 " ).addClass( " txt " ); // 页面加载完成之后,给文本框添加样式:txt,并设置文本框的值
// 获得焦点
$( " input " ).focus( function () {
if ($( " input " ).val() == " 请输入关键字 " ) {
$( " input " ).attr( " class " , " txtEnter " ).val( "" ); // 如果用户未输入关键字,样式表重置为class=txtEnter
}
})
// 失去焦点
$( " input " ).blur( function () {
if ($( " input " ).val() == "" ) {
$( " input " ).attr( " class " , " txt " ).val( " 请输入关键字 " ); // 如果文本框值为空,则重置样式表为txt
}
})
})
</ script >
< style type ="text/css" >
.txt
{
color : Gray ;
}
.txtEnter
{
color : Black ;
}
</ style >
$( function () {
$( " input " ).val( " 请输入关键字 " ).addClass( " txt " ); // 页面加载完成之后,给文本框添加样式:txt,并设置文本框的值
// 获得焦点
$( " input " ).focus( function () {
if ($( " input " ).val() == " 请输入关键字 " ) {
$( " input " ).attr( " class " , " txtEnter " ).val( "" ); // 如果用户未输入关键字,样式表重置为class=txtEnter
}
})
// 失去焦点
$( " input " ).blur( function () {
if ($( " input " ).val() == "" ) {
$( " input " ).attr( " class " , " txt " ).val( " 请输入关键字 " ); // 如果文本框值为空,则重置样式表为txt
}
})
})
</ script >
< style type ="text/css" >
.txt
{
color : Gray ;
}
.txtEnter
{
color : Black ;
}
</ style >