跨浏览器实现placeholder效果的jQuery插件
曾经遇到这样一个问题,处理IE8密码框placeholder属性兼容性。几经周折,这个方案是可以解决问题的。
1、jsp页面引入js插件
<script type="text/javascript" src="<%=basePath%>/placeholder.js" ></script>
2、页面初始化代码
<script type="text/javascript">
//input提示信息 效果插件 针对ie8处理
$(function() {
if(!('placeholder' in document.createElement('input'))){ $('#password').placeholder({isUseSpan:true});
} }); </script>
3、页面标签代码
<input id="password" name="j_password" class="input_text input_open" placeholder="请输入密码" type="password" >
4、插件placeholder.js
/**
* jQuery EnPlaceholder plug
* 跨浏览器实现placeholder效果的jQuery插件
* version 1.0
* }
*/
//
// var defaultValue = "账号";
(function ($) {
$.fn.extend({
"placeholder":function (options) {
options = $.extend({
placeholderColor:'#BABABA', //#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; left:85px; 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);
5、GAME OVER
跨浏览器实现placeholder效果的jQuery插件的更多相关文章
- IE下支持文本框和密码框placeholder效果的JQuery插件
基于jQuery实现的,主要用于IE下实现placeholder效果,可同时支持文本和密码输入框.placeholder是HTML5新增的一个属性,当input设置了该属性后,该值的内容将作为灰色提示 ...
- 跨浏览器的placeholder-jQuery版(jQuery插件EnPlaceholder)
案例:整搜索框,需要默认占位符为"请输入关键词",获取焦点时,占位符消失或不可用(不影响正常输入),丢失焦点后,若用户无内容输入,占位符继续出现,继续占位.这种代码我想前端们已经很 ...
- 【jquery】基于 jquery 实现 ie 浏览器兼容 placeholder 效果
placeholder 是 html5 新增加的属性,主要提供一种提示(hint),用于描述输入域所期待的值.该提示会在输入字段为空时显示,并会在字段获得焦点时消失.placeholder 属性适用于 ...
- jQuery实现ie浏览器兼容placeholder效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 跨浏览器的placeholder – 原生JS版
转自来源 : http://www.ifrans.cn/placehoder/ 跨浏览器的placeholder – 原生JS版 html5为input元素新增了一个属性”placeholder”,提 ...
- Stickup – 轻松实现元素固定效果的 jQuery 插件
粘贴是一个简单的 jQuery 插件,在页面滚动的时候固定一个元素到浏览器窗口的顶部,让其总是保持在视图中可见.这个插件作用于多页的网站,但是对于单页的布局有额外的功能.借助 CSS,还可以实现当前视 ...
- 20 个用于处理页面滚动效果的 jQuery 插件
对设计和开发一个网站来说,web开发者不能低估了网站滚动效果的重要性.如今,设计者们都视为一大挑战了,在网站中设计出吸引眼球的高效视觉滚动效果.幸运的是有各种各样的jquery 滚动插件可供他们使用, ...
- 高仿阴阳师官网轮播图效果的jQuery插件
代码地址如下:http://www.demodashi.com/demo/12302.html 插件介绍 这是一个根据阴阳师官网的轮播效果所扒下来的轮播插件,主要应用于定制个性化场景,目前源码完全公开 ...
- javascript设计模式实践之职责链--具有百叶窗切换图片效果的JQuery插件(三)
在上一篇<javascript设计模式实践之模板方法--具有百叶窗切换图片效果的JQuery插件(二)>里,通过采用模板方法模式完成了切换效果对象的构建编写. 接下来就是完成各效果对象的调 ...
随机推荐
- 购物车动画(Android)
购物车动画(Android) 前言:当我们写商城类的项目的时候,一般都会有加入购物车的功能,加入购物车的时候会有一些抛物线动画,最近做到这个功能,借助别人的demo写了一个. 效果: 开发环境:And ...
- CVE-2015-1642 POC
月初,玄武实验室的“每日安全动态”推送了一篇office UAF漏洞利用的文章,之前对office上UAF漏洞利用占位问题有些疑问,刚好就借助这篇文章重现了一下.其中堆喷射部分不是特别稳定,漏洞成因和 ...
- error LNK1104: 无法打开文件“libzmq.lib”
vs 错误: error LNK1104: 无法打开文件“libzmq.lib” 解决方法: 你自己新建项目后,需要操作 项目>属性>链接器>常规>附加库目录>,然后把 ...
- [EffectiveC++]item38:通过复合塑膜出has -a 或“根据某物实现出”
Model "has-a"or “is-implemented-in-terms-of” through composition
- GitHub & Git 基础 (YouTube中文翻译版)
GitHub & Git 基础系列视频 播放列表 由于视频资源在YouTube上,可能需要FQ:https://code.google.com/p/chromeplus/ 视频包括以下内容 1 ...
- 不错的.net开源项目
Json.NET http://json.codeplex.com/ Json.Net是一个读写Json效率比较高的.Net框架.Json.Net 使得在.Net环境下使用Json更加简单.通过Lin ...
- mongoDB 创建数据库、删除数据库
创建数据库 use 命令 MongoDB 用 use + 数据库名称 的方式来创建数据库.use 会创建一个新的数据库,如果该数据库存在,则返回这个数据库. 语法格式 use 语句的基本格式如下: u ...
- std::vector的内存释放
先上一段代码 using namespace std; class A{ public: ~A(){ cout << "deconstruct"; }; }; #inc ...
- 在Node中使用ES7新特征——async、await
async与await两个关键字是在ES7中添加的新特征,旨在更加直观的书写异步函数,避免出现callback hell. callback hell是什么? readFileContents(&qu ...
- ListView实现分页加载(一)制作Demo
一.什么是分页加载 在下面的文章中,我们来讲解LitView分页加载的实现.什么是分页加载呢?我们先看几张效果图吧,如下: ...