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. 利用glibc中锁结构的信息解决死锁问题

       首先非常感谢老丁和老李同学的帮助,没有他们这个问题估计又得搞很久.遇见这个问题,真是头疼.不熟悉代码.不熟悉流程,但是领导还是把活给排下来了(实在不解),只能硬着头皮找了. 问题是这样的,cac ...

  2. 将String转化为Long,并将Long转化为Date

    package org.ljh.test.javaee; import java.text.SimpleDateFormat; import java.util.Date; public class ...

  3. BZOJ NOI十连测 第一测 T2

    思路:看到这题,就感觉是一道很熟悉的题目: http://www.cnblogs.com/qzqzgfy/p/5535821.html 只不过这题的K最多可以到N,而且边权不再只是1,考试的时候yy了 ...

  4. keil TEA

    http://bbs.mydigit.cn/read.php?tid=545086 #include "reg52.h" void send_char(unsigned char ...

  5. C语言预处理命令总结大全

    C程序的源代码中可包括各种编译指令,这些指令称为预处理命令.虽然它们实际上不是C语言的一部分,但却扩展了C程序设计的环境.本节将介绍如何应用预处理程序和注释简化程序开发过程,并提高程序的可读性.ANS ...

  6. javascript全局对象

    一.Array 二.Boolean 三.Date 四.Error 五.EvalError 六.Function 七.JSON 八.Math 九.Number 十.Object 十一.RangeErro ...

  7. 弄明白Android 接口回调机制

    以前对于这个机制理解不够深刻,现在重新整理下思路. 一.建模 我理解的接口回调就是,我这个类实现了一个接口里的方法doSomething,然后注册到你这里,然后我就去做别的事情去了,你在某个触发的时机 ...

  8. Linux系统编程(18)——正则表达式实用举例

    匹配特定字符串: 只能输入长度为3的字符:"^.{3}$". 只能输入由26个英文字母组成的字符串:"^[A-Za-z]+$". 只能输入由26个大写英文字母组 ...

  9. 安全运维之:Linux系统账户和登录安全

    一.合理使用Shell历史命令记录功能 在Linux下可通过history命令查看用户所有的历史操作记录,同时shell命令操作记录默认保存在用户目录下 的.bash_history文件中,通过这个文 ...

  10. mongodb debug

    1,MongoDb log use local; db.startup_log.find();