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 ...
随机推荐
- Supervisor安装与配置
Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统 ...
- java后端时间处理工具类,返回 "XXX 前" 的字符串
转自:https://www.cnblogs.com/devise/p/9974672.html 我们经常会遇到显示 "某个之间之前" 的需求(比如各种社交软件,在回复消息时,显示 ...
- XE7 数据库独立运行需要的文件
dbxase.dlldbxmss.dlldbxmss9.dlllibeay32.dllmidas.dllMSVCR100.DLL sqlncli10.dllssleay32.dll
- Spring Boot SSO单点登入
https://github.com/ITDragonBlog/daydayup/tree/master/SpringBoot-SSO 流程图: 1: Redis 保存用户信息 到Redis(KEY- ...
- cannot nest '/dubboService/src/main/resources' inside '/dubboService/src/main' .To enable the nesting exclude '/resources' from '/dubboService/src/main'
eclipse Maven--->update Project时出现以上错误: cannot nest '/dubboService/src/main/resources' inside '/d ...
- Selenium+TestNG+Maven 搭建
Java环境配置 Eclipse配置TestNG Eclipse配置Maven pom.xml文件相关配置,添加依赖selenium和TestNg的jar包 <!-- https://mvnre ...
- Python_05-文件操作
目录: 1 文件操作 1.1 快速入门 1.1.1 用Python创建一个新文件 1.1.2 文件内容追加,从0到9的10个随机整数 ...
- Visual Studio C++ include与library
首先介绍几种目录: 1. 系统路径 系统路径在vc中是"Properties->Configuration Properties -> VC++ Directories" ...
- 74款安卓和IOS app源码地址
知乎专栏App https://github.com/bxbxbai/ZhuanLan WeChat高仿微信 项目地址: https://github.com/motianhuo/wechat Gan ...
- IntelliJ IDEA 2017版开发SpringBoot之fastJsonHttpMessageConvert使用
继承WebMvcConfigurerAdapter,改写成自己的json转换工具的写法 1.建立实体类 package com.fastjson; import com.alibaba.fastjso ...