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 ...
随机推荐
- (原+转)pycharm中传入命令行参数
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5670821.html 参考网址: http://zhidao.baidu.com/question/5 ...
- MVC 模型、视图、控制及其单入口文件的mvc的工作原理
什么是mvc,mvc就是模型视图控制,模型就是model,在项目中负责数据库相关的操作,视图就是view ,负责页面的展示和数据的展示,控制就是controller ,负责中间的逻辑转换,数 ...
- java poi 合并单元格后边框问题
在项目中用poi合并单元格,但发现边框会有不显示的问题. 在网上搜集了答案,来记录一下. 解决方法: 将每个没用到的单元格都设空值. 例如: HSSFCell cell = row.createCel ...
- css3 页面退出和进入的动画
@-webkit-keyframes slideIn { from { -webkit-transform: translate3d(100%,0,0); transform: translate3d ...
- CSS样式中,background-image 背景图片居中显示并且在不同屏幕分辨率下始终居中
body { margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; background-colo ...
- C语言数据类型转换
变量的数据类型是可以转换的.转换的方法有两种,一种是自动转换,一种是强制转换. 自动转换 自动转换发生在不同数据类型的量混合运算时,由编译系统自动完成.自动转换遵循以下规则: 若参与运算量的类型不同, ...
- Js验证 :只能输入数字和小数点 验证是否是数字 js取float型小数点后两位
JS判断只能是数字和小数点 1.文本框只能输入数字代码(小数点也不能输入)<input onkeyup="this.value=this.value.replace(/\D/g,'') ...
- keil禁止给uncalled segment分配空间
把target options中的device页中选上"Use LX51 ...",然后在LX51 Misc页中的Misc Control中填入"REMOVEUNUSED ...
- TCP连接探测中的Keepalive和心跳包. 关键字: tcp keepalive, 心跳, 保活
1. TCP保活的必要性 1) 很多防火墙等对于空闲socket自动关闭 2) 对于非正常断开, 服务器并不能检测到. 为了回收资源, 必须提供一种检测机制. 2. 导致TCP断连的因素 如果网络正常 ...
- html模块一些方法
<pre name="code" class="python"> find_by_tag_name: @elements = $h->find ...