jquery实用技巧之输入框提示语句
作者:Marksinoberg 时间:2024-04-22 12:49:10
我们在编写网页的时候不可避免的会遇到输入框,那么怎么设计输入框才能更加优雅呢?不同的人会有不同的答案,下面分享一个比较不错的设计。
效果图
细节
这个效果主要是通过JQuery来实现,我的思路如下:
输入框获取鼠标焦点之前,显示原标签的value属性值;获取了鼠标焦点之后,如果当前value有值,那就清空,否则恢复;密码框特殊照顾,待会讲。
实现的代码如下:
$("#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("请输入邮箱地址")
}
});
完整的小例子
完整的代码如下,尤其注意<input type="text" id="password">的实现!
<!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(){});其就是$(document).ready(function(){});的缩写。这个倒不是什么重点。
$(this).attr(“type”,”password”);将当前对象(也就是获取鼠标焦点的输入框)的属性值进行动态的改变。达到输入数据的时候以密码框的形式出现。
标签:jquery,输入框,提示
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
js控制表单不能输入空格的小例子
2013-07-21 11:58:51
python实现音乐播放和下载小程序功能
2023-07-03 17:59:03
![](https://img.aspxhome.com/file/2023/5/105855_0s.jpg)
python使用opencv切割图片白边
2021-10-16 04:44:06
![](https://img.aspxhome.com/file/2023/9/128509_0s.jpg)
Python爬取网页信息的示例
2021-02-09 10:43:21
![](https://img.aspxhome.com/file/2023/9/71549_0s.png)
mysql 5.5 开启慢日志slow log的方法(log_slow_queries)
2024-01-15 15:05:36
![](https://img.aspxhome.com/file/2023/9/95629_0s.jpg)
PHP addcslashes()函数讲解
2023-06-10 01:32:33
![](https://img.aspxhome.com/file/2023/9/55399_0s.png)
通过mysql-proxy完成mysql读写分离
2024-01-24 03:13:09
解决pymysql cursor.fetchall() 获取不到数据的问题
2024-01-29 09:00:19
![](https://img.aspxhome.com/file/2023/0/94700_0s.jpg)
C#向数据库中插入或更新null空值与延迟加载lazy
2024-01-24 09:28:20
python中如何正确使用正则表达式的详细模式(Verbose mode expression)
2022-08-28 04:07:02
![](https://img.aspxhome.com/file/2023/3/79753_0s.png)
python标识符的用法及注意事项
2023-05-02 03:03:04
Python 中urls.py:URL dispatcher(路由配置文件)详解
2021-01-19 02:47:15
![](https://img.aspxhome.com/file/2023/3/101193_0s.png)
JavaScript/jQuery实现切换页面效果
2024-04-22 22:23:17
![](https://img.aspxhome.com/file/2023/8/135828_0s.jpg)
网页特效文字之—粗糙字
2013-07-23 04:34:56
![](https://img.aspxhome.com/file/UploadPic/20072/2007231164332s.jpg)
uni-app的基本使用教程
2024-05-11 09:15:18
Django的restframework接口框架自定义返回数据格式的示例详解
2023-06-13 05:13:51
![](https://img.aspxhome.com/file/2023/4/60954_0s.png)
解决golang编译提示dial tcp 172.217.160.113:443: connectex: A connection attempt failed(推荐)
2023-07-16 04:24:49
Python读取stdin方法实例
2022-09-07 19:03:00
![](https://img.aspxhome.com/file/2023/9/86159_0s.png)
Python如何用NumPy读取和保存点云数据
2022-05-26 17:41:19
![](https://img.aspxhome.com/file/2023/7/96917_0s.png)
对python3 Serial 串口助手的接收读取数据方法详解
2023-05-31 17:39:01