这次给大家带来如何操作JS实现html中placeholder属性提示文字,操作JS实现html中placeholder属性提示文字的注意事项有哪些,下面就是实战案例,一起来看一下。
如何通过js实现html的placeholder属性效果呢
我们需要这样做:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <!DOCTYPE html>
<html>
<head>
<meta charset= "utf-8" >
<title>JS实现placeholder属性效果</title>
<script>
function bl(){
var a=document.getElementById( "inpt" );
if (a.value.length<=0){
a.style.color= "#999999" ;
a.value= "请输入姓名" ;
}
}
function fo(){
var a=document.getElementById( "inpt" );
if (a.value== "请输入姓名" ){
a.style.color= "black" ;
a.value= "" ;
}
}
</script>
</head>
<body>
<input style= "color: #999999;" value= "请输入姓名" id= "inpt" type= "text" onblur= "bl()" onfocus= "fo()" />
</body>
</html>
|
登录后复制
运行效果如下:

补充:
这里再为大家补充一个jQuery实现的placeholder属性效果示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!DOCTYPE html>
<html>
<head>
<meta charset= "utf-8" >
<title>www.jb51.net jQuery实现placeholder属性效果</title>
<script src= "http://libs.baidu.com/jquery/2.0.0/jquery.min.js" ></script>
</head>
<body>
<input style= "color: #999999;" data-value= "请输入姓名" id= "inpt" type= "text" />
<script>
function placeHolder(event){
var self = $(this), selfDataValue = self.attr( "data-value" ), selfValue = self.val();
if (selfDataValue){
event.type == "click" ? (selfValue == selfDataValue && (self.val( "" ).css( "color" , "#333" ))) : (event.type == "blur" && (selfValue == "" && (self.val(selfDataValue).css( "color" , "#A9A9A9" ))))
} else {
return false;
}
}
$( "#inpt" ).on( "click blur" ,placeHolder);
</script>
</body>
</html>
|
登录后复制
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
怎样利用Vue写一个双向数据绑定
怎样使用Vue实现倒计时按钮
以上就是如何操作JS实现html中placeholder属性提示文字的详细内容,更多请关注php中文网其它相关文章!