我们来认识下input输入框的placeholder属性。

placeholder 属性提供可描述输入字段预期值的提示信息。(placeholder 属性适用于以下的 <input> 类型:text, search, url, telephone, email 以及 password)

该提示会在输入字段为空时显示,并会在字段获得焦点时消失(IE10+获取焦点消失,Chrome,FF等在输入有值时消失)。

IE10+,Chrome,Firefox,Safari支持placeholder属性(IE6/7/8/9不支持)。 

在页面显示类似:

html代码:

为了让IE6/7/8/9支持placeholder属性,我说说自己解决方法。

首先判断浏览器是否支持placeholder属性。

var isSupport = function(){
return 'placeholder' in document.createElement('input');
}

  如果不支持,其中分两种情况:

  如果是密码框(type="password"),就创建一个类似的input标签并且设置(type="text"),把原来有placeholder属性的input标签隐藏,并且把placeholder的值赋给新建的input标签,最后把新建的input标签插入到原来的input标签后面。

if(input.type == 'password'){
var newInput = document.createElement('input');
newInput.type = 'text';
newInput.value = input.placeholder;
input.style.display = none;
input.parentNode.insertBefore(newInput,input.nextSibling);
}

  如果是一般文本框(type="text")或者其他类型 search, url, telephone, email,就设置input的值等于placeholder属性的值。

if(input.type == 'text'){
input.value = input.placeholder;
}

然后是获得焦点时:

  密码框类型是新建input标签获得焦点,隐藏新建input标签,显示原来的密码框。

newInput.onfocus = function(){
newInput.style.display = 'none';
input.style.display = '';
input.focus();
}

  其他类型获得焦点,清空设置的value值。

input.onfocus = function(){
if(input.value == input.placeholder) input.value = '';
}

最后是失去焦点时:

  密码框类型是判断原有的input失去焦点,如果有用户输入的值,不做任何改变,如果没有就隐藏,然后显示新建的input标签。

input.onblur = function(){
if(input.value == ''){
newInput.style.display = '';
input.style.display = 'none';
}
}

  其他类型失去焦点判断用户是否有输入的值,如果没有,就设置value值为placeholder的值,如果有就不做任何改变。

input.onblur = function(){
if(input.value == '') input.value = input.placeholder;
}

总的来说分两块处理,密码类型和非密码类型。

为了方便,兼容各大浏览器,一般要封装成一个插件,下面是我的一个封装,供参考。

/**
* LBS PlaceHolder
* ============================================
* 直接调用:
* PlaceHolder.init() //页面所有input
* PlaceHolder.create(input) //单个或多个input
* ============================================
* PlaceHolder.className
* 为显示占位文本input添加类名 默认placeholder
* ============================================
**/ ;(function(){
var PlaceHolder = {
_support: (function(){
return 'placeholder' in document.createElement('input');
})(),
className: 'placeholder',
init: function(){
if(!PlaceHolder._support){
var inputs = document.getElementsByTagName('input');
PlaceHolder.create(inputs);
}
},
create: function(inputs){
if(PlaceHolder._support) return;
var input = null, i = 0, n = inputs.length, holds = [];
if(!n) inputs = [inputs];
for(; i 有两种用法: 1. 页面所有input标签
PlaceHolder.init();

2.单个或者多个的input标签

var input = document.getElementById('username_input');
PlaceHolder.create(input);
var inputs = document.getElementsByTagName('input');
PlaceHolder.create(inputs);

其中有个 PlaceHolder.className , 这是个类名引用,默认类名称是 placeholder 

为什么要加这个类名呢?主要是为了设置placeholder占位文本在input标签中的颜色。

为了得到一致的占位文本颜色,需要设置样式(假设为红色)

/*输入时的颜色*/
input{color: #000;}
/*占位时的颜色*/
input.placeholder{color: #f00;}/* IE6/7/8/9 */
input::-webkit-input-placeholder{ color:#f00;}/* WebKit */
input:-moz-placeholder{color:#f00;}/* Firefox 4 - 18 */
input::-moz-placeholder{color:#f00;}/* Firefox 19+ */
input:-ms-input-placeholder{color:#f00;}/* IE10+ */

有时候会发现设置的颜色没有起作用,注意下CSS样式的优先级。

可以在各个浏览器看看效果:

/*输入时的颜色*/
#testPlaceHolder input{padding: 5px;margin: 5px;color: #000;}
/*占位时的颜色*/
#testPlaceHolder input.placeholder{color: #f00;}/* IE6/7/8/9 */
#testPlaceHolder input::-webkit-input-placeholder{ color:#f00;}/* WebKit */
#testPlaceHolder input:-moz-placeholder{color:#f00;}/* Firefox 4 - 18 */
#testPlaceHolder input::-moz-placeholder{color:#f00;}/* Firefox 19+ */
#testPlaceHolder input:-ms-input-placeholder{color:#f00;}/* IE10+ */

到这里,差不多解决了各个浏览器placeholder问题,其实仔细点会发现一些差别。

支持placeholder的(IE10+获取焦点消失,Chrome,FF等在输入有值时消失)

插件是获取焦点消失,为了某些人要所有浏览器一致的要求,得做出一些改变,原理也差不多。

世界本来是丰富多彩的,不同的浏览器不同的体验有什么不好呢?

认识input输入框的placeholder属性的更多相关文章

  1. input输入框的readonly属性-----http://www.w3school.com.cn/tags/tag_input.asp

    http://www.w3school.com.cn/tags/tag_input.asp input输入框的readonly属性 查询方法: 1.先找官方的文档,api 2.官方的有看不懂的再百度相 ...

  2. HTML5 input控件 placeholder属性

    placeholder 属性提供可描述输入字段预期值的提示信息(hint),该提示会在输入字段为空时显示,并会在字段获得焦点时消失. <input placeholder="请输入姓名 ...

  3. input date 支持placeholder属性

    第一种解决方法:IE,火狐浏览器,不支持input date的日历功能,火狐支持日历功能   ie,火狐,谷歌显示placeholder属性 css代码 #dateofday:before{  col ...

  4. input输入框的border-radius属性在IE8下的完美兼容

    在工作中我们发现搜索框大部分都是有圆角的,为此作为经验不足的前端人员很容易就想到,给input标签添加border-radius属性不就解决了嘛.不错方法确实是这样,但是不要忘了border-radi ...

  5. jquery 修改input输入框的 readOnly属性 && input输入框隐藏

    html的代码 <div class="control-group"> <label class="control-label required&quo ...

  6. angular4 form 表单中 input输入框的disabled属性

    直接加[disabled]="isDisabled"属性的话,出现报错 根据提示,做如下修改 private isEdit: boolean = true; private isD ...

  7. 用in判断input中的placeholder属性是否在这个对象里

    <input id="test"> var ele = document.getElementById("test"); if("plac ...

  8. 移动端input中的placeholder属性垂直

    今天做项目时发现,在手机端用placeholder时,Android手机可以垂直显示:ISO则不能使placeholder垂直;解决办法: .gcddfadf-con-pay-1 input::-we ...

  9. 让div支持placeholder属性/模拟输入框的placeholder属性

    实现方式:css div:empty:before{ content: attr(placeholder); color:#bbb;}div:focus:before{ content:none; }

随机推荐

  1. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  2. HDSF主要节点解说(二)工作原理

    HDFS(Hadoop Distributed File System )Hadoop分布式文件系统. 是依据google发表的论文翻版的.论文为GFS(Google File System)Goog ...

  3. OSPF理论总结

    OSPF学习总结一.OSPF协议的报文类型: 1. Hello 报文:主要用来发现.建立和维护邻居关系. 2. DD报文:数据库的描述报文,主要用来两台路由器的数据库同步. 3. LSR报文:链路状态 ...

  4. The Building Blocks-Enterprise Applications Part 2- Information Management and Business Analytics

    1. Business Analytic Applications Data Analytics Also referred to as 'Business Analytics' or 'Busine ...

  5. shell统计

    for i in `ls -r *_*.csv`;do cat $i|echo $i": "`wc -l`;done>tongji.txt

  6. 【Oracle】物理体系结构

     一.ORACLE 物理体系结构 原理结构图 各部分解释: PGA: 私有内存区,仅供当前发起用户使用. 三个作用 用户登录后的session信息会保存在PGA. 运行排序.假设内存不够,orac ...

  7. WPF用SkewTransform画3D柱状图

    WPF用SkewTransform画3D柱状图 SkewTransform主要是对控件实现一种2-D扭曲,具体内容可以查看以下链接: http://msdn.microsoft.com/zh-cn/l ...

  8. 怎样获取android手机联系人并按字母展示(一)

    android提供了本地数据库的查询uri,能够查询出数据: 採用一个AsyncQueryHandler来进行查询, AsyncQueryHandler自己开启了线程来进行数据查询,非常方便 prot ...

  9. Google 开源项目的风格指南

    谷歌C++代码风格指南.农业所需的代码.更难得的是不FQ,决定性的最爱!! . http://zh-google-styleguide.readthedocs.org/en/latest/google ...

  10. CSS检测的高像素密度屏幕设备

    iPhone4尽管是640px解析度,但它的屏幕宽度(device-width)目前只有320px和iPhone3G相同.只是iPhone4S的像素密度2. 然后使用meta viewport什么时候 ...