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【超不错】的更多相关文章

  1. 兼容ie浏览器的placeholder的几种方法

    项目中遇到的问题,试了几种方法,今天整理出来,如果有不合适的地方,希望大家多多提意见. 第一种方法是:使用html新增的属性 “data-”来实现的,实现的时候,input框没有使用placehole ...

  2. css样式兼容不同浏览器问题解决办法

    在网站设计的时候,应该注意css样式兼容不同浏览器问题,特别是对完全使用DIV CSS设计的网,就应该更注意IE6 IE7 FF对CSS样式的兼容,不然,你的网乱可能出去不想出现的效果! 所 有浏览器 ...

  3. 兼容主流浏览器的CSS透明代码

    透明往往能产生不错的网页视觉效果下面是兼容主流浏览器的CSS透明代码.transparent_class {  filter:alpha(opacity=50);  -moz-opacity:0.5; ...

  4. 一个酷炫的,基于HTML5,Jquery和Css的全屏焦点图特效,兼容各种浏览器

    基于HTML5和CSS的焦点图特效,梅花图案的背景很有中国特色,而且还会动哦,效果超炫,推荐下载! 演示图 html代码 <!DOCTYPE html PUBLIC "-//W3C// ...

  5. (转)兼容主流浏览器的CSS透明代码

    透明往往能产生不错的网页视觉效果下面是兼容主流浏览器的CSS透明代码.transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; - ...

  6. 图片旋转+剪裁js插件(兼容各浏览器) « 张鑫旭-鑫空间-鑫生活

    图片旋转+剪裁js插件(兼容各浏览器) « 张鑫旭-鑫空间-鑫生活 图片旋转+剪裁js插件(兼容各浏览器) by zhangxinxu from http://www.zhangxinxu.com 本 ...

  7. 兼容所有浏览器的CSS3圆角

    兼容所有浏览器的CSS3圆角      解决CSS3圆角兼容所有浏览器的方法.本文提到了一种很不错的实现跨浏览器圆角的解决方案,但是说的不够全面,前端观察最近将整理更多更全面的资源给大家,敬请期待. ...

  8. 兼容各浏览器的css背景图片拉伸代码

    需要用到背景图拉伸,找到了下面这段css代码: filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='***.jpg' , s ...

  9. bootstrap3兼容ie8浏览器

    bootstrap3 兼容IE8浏览器 2016-01-22 14:01 442人阅读 评论(0) 收藏 举报  分类: html5(18)    目录(?)[+]   近期在使用bootstrap这 ...

随机推荐

  1. mp4下载完后才能播放的问题

    下载完后才能播放的问题.mp4视频有metadata,通常在文件尾部,而flash读到这个metadata才开始播放,解决办法是用工具处理一下mp4,把它的metadata移至文件头部. 推荐工具:m ...

  2. Linq 数据合并,比较,连接,交叉 维恩图工具

    Except 返回包含两个不同之处的linq结果集 Intersect 返回两个容器中共同的数据项 Union 返回所有成员,相同的成员出现多次,将只返回一个 Concat 返回所有数据项

  3. iOS9 App Thinning(应用瘦身)功能介绍

    iOS9 发布后,产生了一个使 App Thinning 无法正常运行的 bug.在iOS9.0.2 版本中,这个 bug 已经被修复,App Thinning 已经可以正常使用.当你从应用商店(Ap ...

  4. codeforces C. DZY Loves Sequences

    http://codeforces.com/contest/447/problem/C 题意:给你n个数的序列,然后让你改变其中的一个数,求得最长上升连续序列的长度值. 思路:先从左边开始求出连续递增 ...

  5. Delphi常用排序

    1.冒泡排序 Delphi/Pascal code   ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 procedure BubbleSort(var x:a ...

  6. 查看Mysql执行计划

    使用navicat查看mysql执行计划: 打开profile分析工具: 查看是否生效:show variable like ‘%profil%’; 查看进程:show processlist; 选择 ...

  7. dotfuscator使用方法

    dotfuscator如何对.net程序进行混淆保护对于程序代码的保护,网上有很多资料,有的说混淆,有的说加密,我比较支持混淆的方法,这样可以让反编译劳工,头晕一阵子,哈哈开玩笑.对于加密如果不是不得 ...

  8. unity5 人皮渲染 Skin Shading

    换了一种方法,优化了一下代码,unity5效果很好,消耗不大 点开可查看大图 加入了次表面散射的阴影与自阴影              ------------by wolf96  wolf_crix ...

  9. Ural-1018 Binary Apple Tree(树形dp+分组背包)

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #i ...

  10. hdu 4284 状态压缩

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4284 #include<cstdio> #include<cstring> # ...