认识input输入框的placeholder属性
我们来认识下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属性的更多相关文章
- 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.官方的有看不懂的再百度相 ...
- HTML5 input控件 placeholder属性
placeholder 属性提供可描述输入字段预期值的提示信息(hint),该提示会在输入字段为空时显示,并会在字段获得焦点时消失. <input placeholder="请输入姓名 ...
- input date 支持placeholder属性
第一种解决方法:IE,火狐浏览器,不支持input date的日历功能,火狐支持日历功能 ie,火狐,谷歌显示placeholder属性 css代码 #dateofday:before{ col ...
- input输入框的border-radius属性在IE8下的完美兼容
在工作中我们发现搜索框大部分都是有圆角的,为此作为经验不足的前端人员很容易就想到,给input标签添加border-radius属性不就解决了嘛.不错方法确实是这样,但是不要忘了border-radi ...
- jquery 修改input输入框的 readOnly属性 && input输入框隐藏
html的代码 <div class="control-group"> <label class="control-label required&quo ...
- angular4 form 表单中 input输入框的disabled属性
直接加[disabled]="isDisabled"属性的话,出现报错 根据提示,做如下修改 private isEdit: boolean = true; private isD ...
- 用in判断input中的placeholder属性是否在这个对象里
<input id="test"> var ele = document.getElementById("test"); if("plac ...
- 移动端input中的placeholder属性垂直
今天做项目时发现,在手机端用placeholder时,Android手机可以垂直显示:ISO则不能使placeholder垂直;解决办法: .gcddfadf-con-pay-1 input::-we ...
- 让div支持placeholder属性/模拟输入框的placeholder属性
实现方式:css div:empty:before{ content: attr(placeholder); color:#bbb;}div:focus:before{ content:none; }
随机推荐
- DOM中的动态NodeList与静态NodeList
GitHub版本号: https://github.com/cncounter/translation/blob/master/tiemao_2014/NodeList/NodeList.md 副标题 ...
- 自己定义 ViewGroup 支持无限循环翻页之三(响应回调事件)
大家假设喜欢我的博客,请关注一下我的微博,请点击这里(http://weibo.com/kifile),谢谢 转载请标明出处,再次感谢 ################################ ...
- 数学之路-python计算实战(19)-机器视觉-卷积滤波
filter2D Convolves an image with the kernel. C++: void filter2D(InputArray src, OutputArray dst, int ...
- Learning React Native笔记
React Native作为一个新事物,相关的资料还不多 官方的文档比较简单,缺少一些系统的例子 在对React Native的应用中,迫切的想学习一些别人的最佳实践.所以想通过看书系统的学习下 之前 ...
- 使用Visual Studio 创建可视Web Part部件
使用Visual Studio 创建可视Web Part部件 可视Web Part部件是很强大的Web 部件.它提供内置设计器创建你的用户界面. 本文主要解说怎样使用Visual Studio 创建可 ...
- linux下Oracle11g RAC搭建(九)
linux下Oracle11g RAC搭建(九) 八.创建ASM仓储 相同在图形化界面操作 [root@node1 ~]# su - grid [grid@node1 ~]$ asmca //创 ...
- matlab secant method
% Matlab script to illustrate the secant method % to solve a nonlinear equation % this particular sc ...
- C# 制作Java +Mysql+Tomcat 环境安装程序,一键式安装
原文:C# 制作Java +Mysql+Tomcat 环境安装程序,一键式安装 要求: JDK.Mysql.Tomcat三者制作成一个安装包, 不能单独安装,安装过程不显示三者的界面, 安装完成要配置 ...
- Android中目的地Intent的使用
一.什么是Intent? Intent的中文意思是目的.在Android中也是“目的”的意思.就是我们要去哪里,从这个activity要前往另一个Activity就需要用到Intent. 示例代码一: ...
- C++-传值与传引用的差别
//值传递与引用传递的差别 #include <iostream> #include <iomanip> using namespace std; void fiddle(in ...