基于JS实现html中placeholder属性提示文字效果示例

作者:包子源 时间:2023-09-07 22:50:58 

本文实例讲述了基于JS实现html中placeholder属性提示文字效果。分享给大家供大家参考,具体如下:

如何通过js实现html的placeholder属性效果呢

我们需要这样做:


<!DOCTYPE html>
<html>
 <head>
   <meta charset="utf-8">
   <title>www.aspxhome.com 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>

运行效果如下:

基于JS实现html中placeholder属性提示文字效果示例

补充:

这里再为大家补充一个jQuery实现的placeholder属性效果示例:


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>www.aspxhome.com 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>

希望本文所述对大家JavaScript程序设计有所帮助。

来源:https://blog.csdn.net/ziwoods/article/details/49815847

标签:JS,placeholder
0
投稿

猜你喜欢

  • 解决vue脚手架项目打包后路由视图不显示的问题

    2024-04-27 16:06:31
  • Python实现绘制多种激活函数曲线详解

    2022-06-30 01:02:06
  • python引入导入自定义模块和外部文件的实例

    2023-01-25 10:02:18
  • web前端vue之CSS过渡效果示例

    2024-04-10 10:33:20
  • SQL Server 数据库索引其索引的小技巧

    2024-01-21 03:13:40
  • 卓越网的配送服务让我很不满意

    2009-03-19 13:49:00
  • Python抓取数据到可视化全流程的实现过程

    2021-06-14 02:03:28
  • python的类class定义及其初始化方式

    2023-08-07 11:52:15
  • wxPython窗口的继承机制实例分析

    2023-03-04 15:55:47
  • Python编程之event对象的用法实例分析

    2023-02-19 09:29:50
  • Python安装spark的详细过程

    2021-05-17 09:59:05
  • Python django框架输入汉字,数字,字符生成二维码实现详解

    2022-12-13 00:23:24
  • python Django框架快速入门教程(后台管理)

    2022-04-17 11:43:12
  • python绘制柱形图的方法

    2022-03-07 07:36:06
  • ubuntu环境下python虚拟环境的安装过程

    2022-07-25 06:09:07
  • MySQL常用命令大全脚本之家总结

    2024-01-15 06:13:13
  • 优化次数过多的循环

    2009-11-12 12:35:00
  • python字典setdefault方法和get方法使用实例

    2023-11-23 21:13:15
  • [机器视觉]使用python自动识别验证码详解

    2021-09-12 03:24:36
  • Python如何telnet到网络设备

    2023-11-20 09:47:45
  • asp之家 网络编程 m.aspxhome.com