html5属性placeholder的js 向下兼容支持(jquery版)
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版)的更多相关文章
- 【学习】jquery.placeholder.js让IE浏览器支持html5的placeholder
type为text或password的input,其在实际应用时,往往有一个占位符,类似这样的: 在没有html5前,一般写成value,用js实现交互,文本框获得焦点时,提示文字消失,失去焦点时,文 ...
- jquery.placeholder.min.js让吃屎的IE浏览器支持placeholder去吧
描述:现在都是HTML5时代了,所有的浏览器都支持placeholder,唯独IE不支持.现在我们有了这款插件,IE下终于可以支持了! 图片展示: 兼容浏览器:IE6+/Firefox/Goog ...
- html5的placeholder属性(IE如何兼容placeholder属性)
界面UI推荐 jquery html5 实现placeholder兼容password IE6 html5的placeholder属性(IE如何兼容placeholder属性) 2013-01-05 ...
- Html5 input placeholder 属性字体颜色修改。
这篇文章主要介绍了有关HTML5 input placeholder 颜色修改方面的知识,需要的朋友可以参考下 Chrome支持input=[type=text]占位文本属性,但下列CSS样式 ...
- HTML5之placeholder属性以及如何更改placeholder属性中文字颜色
今天在群里看到群友问了一个这样的问题,就是如何更改placeholder属性中文字的颜色,以前用过这属性,却是没更改过颜色,于是便试了试,中途遇到些问题,查找资料后特来总结一下. 熟悉HTML5的人应 ...
- 转: HTML5之placeholder属性以及如何更改placeholder属性中文字颜色
今天在群里看到群友问了一个这样的问题,就是如何更改placeholder属性中文字的颜色,以前用过这属性,却是没更改过颜色,于是便试了试,中途遇到些问题,查找资料后特来总结一下. 熟悉HTML5的人应 ...
- 用jquery实现html5的placeholder功能
html5的placeholder功能在表单中经常用到,它主要用来提示用户输入信息,当用户点击该输入框之后,提示文字会自动消失. 我们用jquery实现类似的功能: 当输入框获得焦点时,清空输入框中的 ...
- js进阶 11-3 jquery中css属性如何操作
js进阶 11-3 jquery中css属性如何操作 一.总结 一句话总结:通过css()方法 1.attr和css是有交叉的,比如width,两者中都可以设置,那么他们的区别是什么? 其实通俗一点 ...
- js进阶 11-2 jquery属性如何操作
js进阶 11-2 jquery属性如何操作 一.总结 一句话总结:jquery中的属性用attr方法表示.jquery中都是方法. 1.jquery中的属性的增删改查操作? 只需要两个方法, at ...
随机推荐
- (原)Eclipse中将JNI生成的so打包成jar的步骤
说明:新人,对java不熟,见谅. 1. 新建工程,添加好native support,写好对应的文件(包括cpp文件,so文件和对应的java文件,此处称对应的java文件为SoJAR.java,) ...
- 看看国外的javascript题目,你能全部做对吗?(分享)
本文转自@Aaron的博客,拿过来分享一下.原文:看看国外的javascript题目,你能全部做对吗? 题目一: (function(){ return typeof arguments; })(); ...
- spl_autoload_register()
5.3版本增加了命名空间prepend函数 <?php // function __autoload($class) {// include 'classes/' . $class ...
- python之第一个例子hello world
python用缩进(四个空格,不是teble)来区分代码块 1. coding=utf-8 字符编码,支持汉字 #!/usr/bin/env python# coding=utf-8print ...
- bootstrap的datetimepicker控件只选择年月的配置
<script src="{% static "jquery/jquery-1.11.3.min.js" %}"></script> & ...
- PMBOK 项目管理 九大知识领域和五大流程 PMI
Project Management Institute.PMI 是世界上最大的非盈利机构,是项目管理领域的领导者.PMI制定项目管理行业标准,带领项目管理的研究并提供项目管理的培训,证书,还有一些加 ...
- Sed 与 Linux 等价命令代码鉴赏(转)
参考了 http://www.chinaunix.net/jh/24/307045.html sed http://bbs.chinauni ...
- 2014.8.3情人节欢乐赛【Benny的农场】
Benny的农场 (farm.pas/.c/.cpp) 时间限制:1s.空间限制:128MB 题目描述: Benny有一片农田需要灌溉.农田的形状为矩形,并被分为许多小块.每一块中都有一些水管.共有1 ...
- Jquery之家5个顶级Material Design框架
谷歌Material Design在如今的前端页面设计中非常流行.Material Design的设计风格向我们展示了一个简单而有内涵的现代UI设计方案. Material Design是如此的简洁美 ...
- Permutation Sequence 解答
Question The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all ...