ie兼容placeholder效果
转载:http://www.jb51.net/article/56244.htm
placeholder是HTML5<input>的属性之一,在不同的浏览器( 支持HTML5的现代浏览器 )中会有略微不同的显示效果:
在Chrome( v31.0.1650.63 m)、Firefox( v21.0 )、360安全( v6.3 极速模式 )中,输入栏获得焦点后,提示文字并不消失,如图( Chrome ):
获得焦点前:

获得焦点时:

偏偏IE11要搞点特殊:
获得焦点前:

获得焦点时:

也就是说获得焦点时提示的文字会消失。
非现代浏览器( 例如 IE6-IE9 )是不支持placeholder属性的。现在用jQuery来使这些非现代浏览器也同样能能实现placeholder的显示效果,第一种方法实现的是IE11这种效果,也就是输入框获得焦点时提示文字会消失;如果要想获得类似Chrome的效果,即输入框获得焦点时提示文字并不消失,可以使用第二种方法。
方法一
效果预览:
http://jsfiddle.net/L57b25yr/embedded/result/
思路是,首先判断浏览器是否支持placeholder属性,如果不支持的话,就遍历所有input输入框,获取placeholder属性的值填充进输入框作为提示信息,同时字体设置成灰色。
当输入框获得焦点( focus )同时输入框内文字等于设置的提示信息时,就把输入框内清空;
当输入框失去焦点( blur )同时输入框内文字为空时,再把获取的placeholder属性的值填充进输入框作为提示信息,同时字体设置成灰色;
当输入框内有输入( keydown )时,此时输入框内的提示信息已经由focus事件清除,此时只需要把字体再恢复成黑色即可。
此方法的缺点是,不适用于加载完DOM时即获得焦点的输入框,因为在用户的角度,加载完页面时看到的获得焦点的那个输入框,它的提示文字是看不到的。
HTML:
<input type="text" id="uname" name="uname" placeholder="请输入用户名"/>
CSS:
.phcolor{ color:#999;}
JS:
|
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
$(function(){ //判断浏览器是否支持placeholder属性 supportPlaceholder='placeholder'in document.createElement('input'), placeholder=function(input){ var text = input.attr('placeholder'), defaultValue = input.defaultValue; if(!defaultValue){ input.val(text).addClass("phcolor"); } input.focus(function(){ if(input.val() == text){ $(this).val(""); } }); input.blur(function(){ if(input.val() == ""){ $(this).val(text).addClass("phcolor"); } }); //输入的字符不为灰色 input.keydown(function(){ $(this).removeClass("phcolor"); }); }; //当浏览器不支持placeholder属性时,调用placeholder函数 if(!supportPlaceholder){ $('input').each(function(){ text = $(this).attr("placeholder"); if($(this).attr("type") == "text"){ placeholder($(this)); } }); }}); |
经过测试可以达到IE11placeholder的显示效果。
方法二
此方法的思路是做一张含有提示文字的图片作为input输入框的背景图,初始时获得焦点同时加载背景图;
背景图如下:

当输入框不为空时,去掉背景图;
当输入框为空时,加载背景图;
当用户键盘按键且输入框不为空( 输入字符 )时,去掉背景图;
当用户键盘按键且输入框为空( 删除字符 )时,加载背景图。
此方法的缺点是:需要为每一个提示文字不同的input制作背景图片,并且设置input的样式。
HTML代码不变。
CSS:
.phbg{ background:url(img/bg.jpg) 0 0 no-repeat;}
JS:
|
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
27
28
29
30
31
32
33
34
35
36
37
38
39
|
$(function(){ //判断浏览器是否支持placeholder属性 supportPlaceholder='placeholder' in document.createElement('input'); if(!supportPlaceholder){ //初始状态添加背景图片 $("#uname").attr("class","phbg"); //初始状态获得焦点 $("#uname").focus; function destyle(){ if($("#uname").val() != ""){ $("#uname").removeClass("phbg"); }else{ $("#uname").attr("class","phbg"); } } //当输入框为空时,添加背景图片;有值时去掉背景图片 destyle(); $("#uname").keyup(function(){ //当输入框有按键输入同时输入框不为空时,去掉背景图片;有按键输入同时为空时(删除字符),添加背景图片 destyle(); }); $("#uname").keydown(function(){ //keydown事件可以在按键按下的第一时间去掉背景图片 $("#uname").removeClass("phbg"); }); }}); |
此方法至此结束。
此方法在IETester的IE8下显示效果:
获得焦点时:

失去焦点时:

有输入时:

ie兼容placeholder效果的更多相关文章
- 【jquery】基于 jquery 实现 ie 浏览器兼容 placeholder 效果
placeholder 是 html5 新增加的属性,主要提供一种提示(hint),用于描述输入域所期待的值.该提示会在输入字段为空时显示,并会在字段获得焦点时消失.placeholder 属性适用于 ...
- jQuery实现ie浏览器兼容placeholder效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- jQuery效果之封装模拟placeholder效果,让低版本浏览器也支持
页面中的输入框默认的提示文字一般使用placeholder属性就可以了,即: <input type="text" name="username" pla ...
- html5的placeholder属性(IE如何兼容placeholder属性)
界面UI推荐 jquery html5 实现placeholder兼容password IE6 html5的placeholder属性(IE如何兼容placeholder属性) 2013-01-05 ...
- DIV实现CSS 的placeholder效果
placeholder是HTML5中input的属性,但该属性并不支持除input以外的元素 但我们可以使用Css before选择器来实现完全相同的效果 <!DOCTYPE html> ...
- IE下支持文本框和密码框placeholder效果的JQuery插件
基于jQuery实现的,主要用于IE下实现placeholder效果,可同时支持文本和密码输入框.placeholder是HTML5新增的一个属性,当input设置了该属性后,该值的内容将作为灰色提示 ...
- 下拉列表模仿placeholder效果
模仿placeholder效果 <select id="IsTop"> <option value="" disabled selected& ...
- Jquery简单的placeholder效果
Jquery简单的placeholder效果 由于IE6-IE9不支持HTML5中的placeholder,所以自己依赖于Jquery简单的写了一个,供参考! 先看看效果吧!如下JSFiddle地址 ...
- 跨浏览器实现placeholder效果的jQuery插件
曾经遇到这样一个问题,处理IE8密码框placeholder属性兼容性.几经周折,这个方案是可以解决问题的. 1.jsp页面引入js插件 <script type="text/java ...
随机推荐
- iis应用程序池假死问题
“Comprehensive orientate 16:05:43 查看原文 IIS貌似问题不少 问:IIS 网站 并发连接线不多,但是运行一段时间后 就非常慢,系统资源占用都正常,一回收应用 ...
- linux下创建具有root权限的账户
http://blog.chinaunix.net/uid-24631445-id-2981034.html
- C# Panel控件截图
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll ")] private static extern bo ...
- Windows Git 服务器 客户端 Delphi Git配置
装Git后本地单机版就有了版本管理功能. git 使用记录 git 客户端 这2个工具足够用. git for windows,http://git-scm.com/download/,Git-1.9 ...
- .net core 一个避免跨站请求的中间件
前提: 前几天看到博客园首页中有这么一篇文章:跨站请求伪造(CSRF),刚好前段时间自己一直也在搞这个东西,后来觉得每次在form表单里添加一个@Html.AntiForgeryToken,在对应的方 ...
- 设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
转自http://unmi.cc/uilable-uitextfield-padding-insets 主要是理解下UIEdgeInsets在IOS UI里的意义. 靠,这货其实就是间隔,起个名字这么 ...
- Linux学习-linux系统下python升级到python3.6步骤详解,以及遇到的问题解决
说明:一般linux会自带pyhton2.7 1.首先下载源tar包 可利用linux自带下载工具wget下载,如下所示: wget http://www.python.org/ftp/python/ ...
- Python_08-常用模块
1 常用模块介绍 1.1 os模块 1.2 sys模块 1.3 built-in内置模块 1.4 time模块 1.5 re模块 2 ...
- php通过反射执行某方法
简单记录下通过反射来获取某方法的参数,然后利用php内置函数类执行此方法 一个简单的test类 class test { //2个参数默认值 public function b($name='lemo ...
- 未能映射路径"/"
1.检查Server.MapPath 这里面需要像这样: ~/uploads/ 有~符号. 2.应用程序池出现问题,换一个应用程序池,或者重启程序池.