jquery placeholder
/*
* @author ambar
* html5 placeholder pollfill
* - 使用绝对定位内嵌层
* - 也适用于密码域
* 目标浏览器: IE 6~9, FF 3.5 */
(function ($) {
var attr = 'nullText'; $.fn.placeholder = function (options) {
return this.each(function () {
var $input = $(this);
if (typeof options === 'string') {
options = { text: options };
} var opt = $.extend({
text: '',
style: {}, namespace: 'nullText',
hideOnFocus: false
}, options || {}); if (!opt.text) {
opt.text = $input.attr(attr);
} var width = $input.width(); var show = function () { $layer.show(); };
var hide = function () { $layer.hide(); };
var is_empty = function () { return !$input.val(); };
var check = function () { is_empty() ? show() : hide(); }; var position = function () {
var pos = $input.position();
if (!opt.hideOnFocus) {
// 按鍵隱藏的情况,需要移動光標两像素
pos.left += 2;
}
pos.top += 3;
$layer.css(pos);
}; var layer_style = {
color: 'gray',
cursor: 'text',
textAlign: 'left',
position: 'absolute',
textShadow: 'none',
fontSize: $input.css('fontSize'),
fontFamily: $input.css('fontFamily'),
display: is_empty() ? 'block' : 'none',
zIndex: '1000',
top: '3px'
}; var package_style = { position: 'relative' }; // create
var layer_props = {
text: opt.text,
width: width,
height: 'auto'
}; // 确保只绑定一次
var ns = '.' + opt.namespace, $layer = $input.data('layer' + ns);
if (!$layer) {
$layer = $('<label>', layer_props)
.attr('for', $input.attr('id'));
$input.data('layer' + ns, $layer);
} var $package = $("<div></div>").css(package_style);
$input.parent().append($package);
$package.append($layer).append($input); // activate
$layer
.css($.extend(layer_style, opt.style))
.unbind('click' + ns)
.bind('click' + ns, function () {
opt.hideOnFocus && hide();
$input.focus();
});
$input
.unbind(ns)
.bind('blur' + ns, check); if (opt.hideOnFocus) {
$input.bind('focus' + ns, hide);
} else {
$input.bind('keypress keydown' + ns, function (e) {
var key = e.keyCode;
if (e.charCode || (key >= 65 && key <= 90)) {
hide();
}
})
.bind('keyup' + ns, check);
} // 由于 ie 记住密码的特性,需要监听值改变
// ie9 不支持 jq bind 此事件
$input.get(0).onpropertychange = check; position();
check();
});
}; })(jQuery); //调用方法
$("[nullText]").placeholder();
jquery placeholder的更多相关文章
- JQuery Placeholder - Input提示信息
JQuery Placeholder Placeholder属性是HTML5为Input添加的,在Input上提供一个占位符,文字形式展示输入文字预期值的提示信息. 如: 需要使用:placehold ...
- jquery.placeholder.js的使用
最近做东西用到placeholder这个属性,可是这个属性在低版本的IE或者QQ浏览器等这些浏览器上这个属性不能生效,后来在网上查了下,发现了jquery的一个插件jquery.placeholder ...
- jquery.placeholder.min.js让吃屎的IE浏览器支持placeholder去吧
描述:现在都是HTML5时代了,所有的浏览器都支持placeholder,唯独IE不支持.现在我们有了这款插件,IE下终于可以支持了! 图片展示: 兼容浏览器:IE6+/Firefox/Goog ...
- 【学习】jquery.placeholder.js让IE浏览器支持html5的placeholder
type为text或password的input,其在实际应用时,往往有一个占位符,类似这样的: 在没有html5前,一般写成value,用js实现交互,文本框获得焦点时,提示文字消失,失去焦点时,文 ...
- jquery 小插件,完成“输入字段预期值的提示信息”,防html5 placeholder属性
前言:在很多时候,我们需要文本框中显示默认值,获取焦点时,文字框中就会清空给的值,当失去焦点时,如果没有值,继续显示默认的文字,如果有输入值,就显示输入的值.现在项目中需要用到这个地方的功能比较多,于 ...
- Jquery插件placeholder的用法
闲的蛋疼,演示一下Jquery插件placeholder的用法,借助该插件能够轻松实现HTML5中placeholder特效: 效果图: 实现代码: <%@ page language=&quo ...
- jQuery插件placeholder的使用方法
借助该插件可以轻松实现HTML5中placeholder特效: 实例代码如下: <script type="text/javascript" src="<%= ...
- 实用的placeholder插件,兼容IE下的placeholder,jquery插件
placeholder在IE下无法兼容 ,下面的插件很好的处理了这个问题,拿去不谢 /* * jQuery placeholder, fix for IE6,7,8,9 * @website itmy ...
- html5 placeholder ie 不兼容问题 解决方案
解决HTML5 placeholder的方案 来源: 时间:2013-09-05 20:06:49 阅读数:11375 分享到: 0 [导读] 使低版本浏览器支持Placeholder有很多方 ...
随机推荐
- 【USACO 2.3】Money Systems(dp)
v种货币,求有多少种组成和为n. dp[i][j]表示前i种货币价格为j有多少种方案,dp[i][j]+=dp[i-1][j-c]. http://train.usaco.org/usacoprob2 ...
- APiCloud真机调试需要注意的几个问题
具体请看官方文档:http://docs.apicloud.com/Dev-Tools/wifi-debug. APiCloud Android手机真机调试,需要首先在手机上安装官网提供的apploa ...
- 给td添加滚动条
<td><div style="overflow-y:scroll;height:330px;">test</div></td>
- 【UOJ#228】基础数据结构练习题 线段树
#228. 基础数据结构练习题 题目链接:http://uoj.ac/problem/228 Solution 这题由于有区间+操作,所以和花神还是不一样的. 花神那道题,我们可以考虑每个数最多开根几 ...
- HTML5 开发框架
WeUI WeUI 是一套同微信原生视觉体验一致的基础样式库,由微信官方设计团队为微信 Web 开发量身设计,可以令用户的使用感知更加统一.包含button.cell.dialog. progress ...
- python2-gst0.10制作静态包的补丁 v1.1
gst制作成了静态库,而python2的gst有多个动态库引用gst的库 因此,想了一个办法将python2所需要的gst打包成一个单独的共享库 办法就是,将python2_gst所有的.so先制作成 ...
- 【ESRI论坛6周年征文】ArcEngine注记(Anno/ Label/Element等)处理专题 -入门篇
原发表于ESRI中国社区,转过来.我的社区帐号:jhlong http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=122097 ----------- ...
- flask-whooshalchemy需要注意的一点
在学习mega—tutorial时全文搜索模块遇到了问题,那就是使用全文搜索查询出来的数据为空的列表,输出了sql语句后发现where后没有条件,困扰了许久,后来才发现是自己不细心,在进行全文索引时应 ...
- TypeError: 'bool' object is not callable g.user.is_authenticated()
此问题查了stackoverflow后知道is_authenticated是一个属性而不是一个方法所以g.user.is_authenticated() 用法会报错
- Web开发技术发展历史
Web开发技术发展历史 来自:天码营 原文:http://www.tianmaying.com/tutorial/web-history Web的诞生 提到Web,不得不提一个词就是"互 ...