兼容IE浏览器的placeholder【超不错】
jQuery EnPlaceholder plug (兼容IE浏览器的placeholder)使用
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
蕃薯耀 2015年9月8日 14:24:47 星期二
http://fanshuyao.iteye.com/
enplaceholder是一个比较好的placeholder插件,可以在不支持placeholder的浏览器中显示提示,如果浏览器本身支持,就不会使用本插件。
插件来源:http://www.ifrans.cn/jquery-enplaceholder-plug/
插件demo:http://www.ifrans.cn/demo/enplaceholder.html
插件有2仲模式,个人喜欢通过插入元素模拟这种
//通过value模拟placeholder$('input').placeholder();
//通过插入元素模拟placeholder$('input').placeholder({isUseSpan:true});
文本框内容改变时placeholder消失:
$('#username2,#password').placeholder({isUseSpan:true});
获得焦点时placeholder消失:
$('#address2').placeholder({isUseSpan:true,onInput:false});
但插件还有一个问题,就是input不使用placeholder时会返回undefined,但本人已经修改问题,增加了如下代码:
//修正无placeholder时,显示undefined问题
if(defaultValue != null && typeof(defaultValue) != "undefined")
该插件属于Jquery插件,依赖Jquery
插件见附件,或下面的源代码。
源代码:
/**
* jQuery EnPlaceholder plug
* EnPlaceholder是一个跨浏览器实现placeholder效果的jQuery插件
* version 1.0
* by Frans.Lee <dmon@foxmail.com> http://www.ifrans.cn
*
* 修正无placeholder显示undefined问题(lqy--http://fanshuyao.iteye.com/)
* var defaultValue = $(_this).attr('placeholder');
* if(defaultValue != null && typeof(defaultValue) != "undefined"){
*
* }
*/
;(function ($) {
$.fn.extend({
"placeholder":function (options) {
options = $.extend({
placeholderColor:'#ACA899',
isUseSpan:false, //是否使用插入span标签模拟placeholder的方式,默认false,默认使用value模拟
onInput:true //使用标签模拟(isUseSpan为true)时,是否绑定onInput事件取代focus/blur事件
}, options); $(this).each(function () {
var _this = this;
var supportPlaceholder = 'placeholder' in document.createElement('input');
if (!supportPlaceholder) {
var defaultValue = $(_this).attr('placeholder');
//修正无placeholder时,显示undefined问题
if(defaultValue != null && typeof(defaultValue) != "undefined"){
var defaultColor = $(_this).css('color');
if (options.isUseSpan == false) {
$(_this).focus(function () {
var pattern = new RegExp("^" + defaultValue + "$|^$");
pattern.test($(_this).val()) && $(_this).val('').css('color', defaultColor);
}).blur(function () {
if ($(_this).val() == defaultValue) {
$(_this).css('color', defaultColor);
} else if ($(_this).val().length == 0) {
$(_this).val(defaultValue).css('color', options.placeholderColor)
}
}).trigger('blur');
} else {
var $imitate = $('<span class="wrap-placeholder" style="position:absolute; display:inline-block; overflow:hidden; color:'+options.placeholderColor+'; width:'+$(_this).outerWidth()+'px; height:'+$(_this).outerHeight()+'px;">' + defaultValue + '</span>');
$imitate.css({
'margin-left':$(_this).css('margin-left'),
'margin-top':$(_this).css('margin-top'),
'font-size':$(_this).css('font-size'),
'font-family':$(_this).css('font-family'),
'font-weight':$(_this).css('font-weight'),
'padding-left':parseInt($(_this).css('padding-left')) + 2 + 'px',
'line-height':_this.nodeName.toLowerCase() == 'textarea' ? $(_this).css('line-weight') : $(_this).outerHeight() + 'px',
'padding-top':_this.nodeName.toLowerCase() == 'textarea' ? parseInt($(_this).css('padding-top')) + 2 : 0
});
$(_this).before($imitate.click(function () {
$(_this).trigger('focus');
})); $(_this).val().length != 0 && $imitate.hide(); if (options.onInput) {
//绑定oninput/onpropertychange事件
var inputChangeEvent = typeof(_this.oninput) == 'object' ? 'input' : 'propertychange';
$(_this).bind(inputChangeEvent, function () {
$imitate[0].style.display = $(_this).val().length != 0 ? 'none' : 'inline-block';
});
} else {
$(_this).focus(function () {
$imitate.hide();
}).blur(function () {
/^$/.test($(_this).val()) && $imitate.show();
});
}
}
}
}
});
return this;
}
});
})(jQuery);
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
蕃薯耀 2015年9月8日 14:24:47 星期二
http://fanshuyao.iteye.com/
兼容IE浏览器的placeholder【超不错】的更多相关文章
- 兼容ie浏览器的placeholder的几种方法
项目中遇到的问题,试了几种方法,今天整理出来,如果有不合适的地方,希望大家多多提意见. 第一种方法是:使用html新增的属性 “data-”来实现的,实现的时候,input框没有使用placehole ...
- css样式兼容不同浏览器问题解决办法
在网站设计的时候,应该注意css样式兼容不同浏览器问题,特别是对完全使用DIV CSS设计的网,就应该更注意IE6 IE7 FF对CSS样式的兼容,不然,你的网乱可能出去不想出现的效果! 所 有浏览器 ...
- 兼容主流浏览器的CSS透明代码
透明往往能产生不错的网页视觉效果下面是兼容主流浏览器的CSS透明代码.transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; ...
- 一个酷炫的,基于HTML5,Jquery和Css的全屏焦点图特效,兼容各种浏览器
基于HTML5和CSS的焦点图特效,梅花图案的背景很有中国特色,而且还会动哦,效果超炫,推荐下载! 演示图 html代码 <!DOCTYPE html PUBLIC "-//W3C// ...
- (转)兼容主流浏览器的CSS透明代码
透明往往能产生不错的网页视觉效果下面是兼容主流浏览器的CSS透明代码.transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; - ...
- 图片旋转+剪裁js插件(兼容各浏览器) « 张鑫旭-鑫空间-鑫生活
图片旋转+剪裁js插件(兼容各浏览器) « 张鑫旭-鑫空间-鑫生活 图片旋转+剪裁js插件(兼容各浏览器) by zhangxinxu from http://www.zhangxinxu.com 本 ...
- 兼容所有浏览器的CSS3圆角
兼容所有浏览器的CSS3圆角 解决CSS3圆角兼容所有浏览器的方法.本文提到了一种很不错的实现跨浏览器圆角的解决方案,但是说的不够全面,前端观察最近将整理更多更全面的资源给大家,敬请期待. ...
- 兼容各浏览器的css背景图片拉伸代码
需要用到背景图拉伸,找到了下面这段css代码: filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='***.jpg' , s ...
- bootstrap3兼容ie8浏览器
bootstrap3 兼容IE8浏览器 2016-01-22 14:01 442人阅读 评论(0) 收藏 举报 分类: html5(18) 目录(?)[+] 近期在使用bootstrap这 ...
随机推荐
- navBar
改变NavgationBar的颜色: [[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]]; 改变NavgationB ...
- MockupBuilder
玩一下,想起了以前公司产品经理作的些事了...
- 【UVA11294】Wedding (2-SAT)
题意: 有N-1对夫妻参加一个婚宴,所有人都坐在一个长长的餐桌左侧或者右侧,新郎和新娘面做面坐在桌子的两侧.由于新娘的头饰很复杂,她无法看到和她坐在同一侧餐桌的人,只能看到对面餐桌的人.任意一对夫妻不 ...
- win7电脑自动关机怎么设置
WIN7系统自带了关机工具的,下面是步骤 1.“开始”-右键点击“计算机”选择“管理”,在左侧界面中选择“任务计划程序”. 2.在右侧界面中选择“创建基本任务”(向导式创建任务,推荐新手使用)或者“创 ...
- h.264并行解码算法2D-Wave实现(基于多核共享内存系统)
cache-coherent shared-memory system 我们最平常使用的很多x86.arm芯片都属于多核共享内存系统,这种系统表现为多个核心能直接对同一内存进行读写访问.尽管内存的存取 ...
- Delphi 版本号(D1到XE6),发现一个delphi.wikia.com网站
Borland Compiler Conditional Defines Edit Talk1 2,909PAGES ONTHIS WIKI Product Name Version Cond ...
- Android开源项目发现---Spinner选择器与日历选择器篇(持续更新)
1. android-times-square Android日历部件 支持选取单个日期,多个日期,及日期区间段和对话框形式显示 项目地址:https://github.com/square/andr ...
- Web Service和ISAPI的区别与联系 转
Web Service和ISAPI的区别与联系 1.Web Service 是一种新的web应用程序分支,他们是自包含.自描述.模块化的应用,可以发布.定位.通过web调用.Web Service ...
- 阿里云如何添加多个网站 for Linux(绑定域名)
我们可以通过.htaccess文件来达到一个空间帮顶多个域名的效果,即域名1访问空间上webroot下的目录1(即二级目录),域名2访问空间上webroot下的目录2,等等.二级目录名为fuli,需要 ...
- 基于curl 的zabbix API调用
1,认证并取得加密字段 curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0&q ...