placeholder是html5表单特性中比较好用的一条,但是苦于其向下兼容性,所以一般要做向下兼容的站点都不敢用,如果有用到的地方,也是用js简单模拟而实现的,那么有没有一个一劳永逸的方法去解决这个问题呢?

接下来我就来带大家实现这个方案:

if ('placeholder' in document.createElement('input')) return;

这句代码的意思是判断是否是支持placeholder属性的,如果支持则return,不执行下面代码。

if (this.type == 'submit' || this.type == 'button' || this.type == 'reset') return;

当然,如果input的type是一个按钮,也不需要placeholder。

接下来,就开始实现了

$.fn.placeholderSupport = function(opts){
opts = $.extend({},opts);
return this.each(function(){
//main code
});
}


上述代码是编写扩展$.fn插件的基本代码,后面方法会被放置进jQuery选择器返回值中,也就是$(选择器)里面直接调用。

由于$()返回可能是个jQuery对象数组,所以插件里面一般会进行each,return是标准写法,让jQuery能链接起来,如$().function()......

基础框架就搭好了,开干!

var _this = $(this);
var placeholderText = _this.attr('placeholder') || '';
if (this.type == 'text' || this.type == 'email') {
this.value = placeholderText;
_this.on('focus', function() {
if (this.value === placeholderText) this.value = '';
}); _this.on('blur', function() {
if (this.value === '') this.value = placeholderText;
});
}

this是each里面的每个input,先得到placeholder的值,并缓存起来。

判断type是'text'||'email'时候执行下面(html5的input type比较多,这里先做了这两个的支持,此处不做email验证);
上面代码是解决普通容器的方法,接下来就是最难缠的type='password'的时候。

var id = this.id;
if (id === '') {
this.id = 'placeholderBuild_' + Base.supportId;
id = 'placeholderBuild_' + Base.supportId;
}
var label = $('<label for="' + id + '">' + placeholderText + '</label>');
var css = {
'left': 5 - $(this).outerWidth(true),
'width': $(this).outerWidth(true),
'height': $(this).outerHeight(true),
'line-height': $(this).outerHeight(true) + 'px',
'position': 'absolute',
opacity: '0.5'
};
var valiM = $('<span></span>').css({
position: 'relative'
});
label.css(css).appendTo(valiM);
valiM.insertAfter($(this)); $(this).on('focus', function() {
label.hide();
}).on('blur', function() {
if (this.value === '') label.show();
});

赋值id,Base是全局变量对象。

大致思路是创建一个空的<span>放到input后面,然后在这个空的span里append个label,并把label 的for id设置成input的id,(for是html5特性,所以此处略矛盾)。

给label赋值宽高,position, left,top,opacity等。

然后继续绑定focus,blur方法。

就这么简单。

调用的时候,直接在

$(function(){
   $('input').placeholderSupport();
});

当有异步dom处input需要支持时候,也只组要找到这个input,然后:
$(input).placeholderSupport();

代码写很老了,没怎么优化过,Base可以用$替换,也可以用自己的全局变量对象搞定。
附上完整源代码:

placeholderSupport: function() {
if ('placeholder' in document.createElement('input')) return this;
if (!Base.supportId) {
Base.supportId = 0;
}
return this.each(function(e) {
Base.supportId++;
if (this.type == 'submit' || this.type == 'button' || this.type == 'reset') return;
var placeholderText = $(this).attr('placeholder') || '';
if (this.type == 'text' || this.type == 'email') {
this.value = placeholderText;
$(this).on('focus', function() {
if (this.value === placeholderText) this.value = '';
}); $(this).on('blur', function() {
if (this.value === '') this.value = placeholderText;
});
} else if (placeholderText !== '' && this.type === 'password') {
var id = this.id;
if (id === '') {
this.id = 'placeholderBuild_' + Base.supportId;
id = 'placeholderBuild_' + Base.supportId;
}
var label = $('<label for="' + id + '">' + placeholderText + '</label>');
var css = {
'left': 5 - $(this).outerWidth(true),
'width': $(this).outerWidth(true),
'height': $(this).outerHeight(true),
'line-height': $(this).outerHeight(true) + 'px',
'position': 'absolute',
opacity: '0.5'
};
var valiM = $('<span></span>').css({
position: 'relative'
});
label.css(css).appendTo(valiM);
valiM.insertAfter($(this)); $(this).on('focus', function() {
label.hide();
}).on('blur', function() {
if (this.value === '') label.show();
});
}
});
}

html5属性placeholder的js 向下兼容支持(jquery版)的更多相关文章

  1. 【学习】jquery.placeholder.js让IE浏览器支持html5的placeholder

    type为text或password的input,其在实际应用时,往往有一个占位符,类似这样的: 在没有html5前,一般写成value,用js实现交互,文本框获得焦点时,提示文字消失,失去焦点时,文 ...

  2. jquery.placeholder.min.js让吃屎的IE浏览器支持placeholder去吧

    描述:现在都是HTML5时代了,所有的浏览器都支持placeholder,唯独IE不支持.现在我们有了这款插件,IE下终于可以支持了!  图片展示:   兼容浏览器:IE6+/Firefox/Goog ...

  3. html5的placeholder属性(IE如何兼容placeholder属性)

    界面UI推荐 jquery html5 实现placeholder兼容password  IE6 html5的placeholder属性(IE如何兼容placeholder属性) 2013-01-05 ...

  4. Html5 input placeholder 属性字体颜色修改。

    这篇文章主要介绍了有关HTML5 input placeholder 颜色修改方面的知识,需要的朋友可以参考下     Chrome支持input=[type=text]占位文本属性,但下列CSS样式 ...

  5. HTML5之placeholder属性以及如何更改placeholder属性中文字颜色

    今天在群里看到群友问了一个这样的问题,就是如何更改placeholder属性中文字的颜色,以前用过这属性,却是没更改过颜色,于是便试了试,中途遇到些问题,查找资料后特来总结一下. 熟悉HTML5的人应 ...

  6. 转: HTML5之placeholder属性以及如何更改placeholder属性中文字颜色

    今天在群里看到群友问了一个这样的问题,就是如何更改placeholder属性中文字的颜色,以前用过这属性,却是没更改过颜色,于是便试了试,中途遇到些问题,查找资料后特来总结一下. 熟悉HTML5的人应 ...

  7. 用jquery实现html5的placeholder功能

    html5的placeholder功能在表单中经常用到,它主要用来提示用户输入信息,当用户点击该输入框之后,提示文字会自动消失. 我们用jquery实现类似的功能: 当输入框获得焦点时,清空输入框中的 ...

  8. js进阶 11-3 jquery中css属性如何操作

    js进阶 11-3  jquery中css属性如何操作 一.总结 一句话总结:通过css()方法 1.attr和css是有交叉的,比如width,两者中都可以设置,那么他们的区别是什么? 其实通俗一点 ...

  9. js进阶 11-2 jquery属性如何操作

    js进阶 11-2  jquery属性如何操作 一.总结 一句话总结:jquery中的属性用attr方法表示.jquery中都是方法. 1.jquery中的属性的增删改查操作? 只需要两个方法, at ...

随机推荐

  1. FTP进行上传下载文件

    1.需要引入外部jar包:commons-net-2.0.jar(或者是子包commons-net-ftp-2.0.jar) 2.需下载ftp服务器 3. 本地电脑访问ftp服务器格式:ftp://用 ...

  2. linux 安装mysqlServer

    先下载mysql安装包 打开 http://dev.mysql.com/downloads/mysql/  选择 linux - Generic 再选择*.tar.gz(最后那两个) 下载完毕后,得到 ...

  3. Qt 自定义事件详细实例(继承QEvent,然后QCoreApplication::postEvent()、sendEvent())

    创建用户事件 创建一个自定义类型的事件,首先需要有一个事件号,其值通常大于QEvent::User.为了传递事件信息,因此必须编写自定义的事件类,该事件类从QEvent继承. 编写用户事件:编写用户事 ...

  4. MATLAB三维曲面

    今天终于测试了,发下来第一张试卷中只会做一小题.我蒙了!!! 所以呢,我现在再做一下,总结总结! 作函数 f(x)=2(x1-1)4+2x22 的三维图. 这道题要用到的知识点有函数meshgrid. ...

  5. UESTC_传输数据 2015 UESTC Training for Graph Theory<Problem F>

    F - 传输数据 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  ...

  6. HDOJ-1010 Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 给出一个n*m的迷宫 X为墙 .为空地 S为起点 D为终点 给出时间T 每走一步花费一单位的时间 走过的空 ...

  7. Linux 时间定时同步操作

    Yum –y install ntp安装时钟同步服务加入开机启动Chkcongfig ntpd on添加自动校对时间,每十分钟校对一次Crontab –e */10 * * * * /usr/sbin ...

  8. 【转】通过 ulimit 改善系统性能

    概述 系统性能一直是一个受关注的话题,如何通过最简单的设置来实现最有效的性能调优,如何在有限资源的条件下保证程序的运作,ulimit 是我们在处理这些问题时,经常使用的一种简单手段.ulimit 是一 ...

  9. IOS使用C#预处理命令,多种SDK共存

    当我们使用Unity接 91,XY助手等等SDK时候. 我们需要使用[DllImport("__Internal")] 来声明一个C++的方法调用. 不同的SDK总会有不同的方法. ...

  10. Mac 删除Openfire

    首先,确保你已经关掉了openfire 打开终端 (在应用程序-->实用工具-->) 输入以下命令 sudo rm -rf /Library/PreferencePanes/Openfir ...