JQuery Placeholder - Input提示信息
JQuery Placeholder
Placeholder属性是HTML5为Input添加的,在Input上提供一个占位符,文字形式展示输入文字预期值的提示信息。
如:
需要使用:placeholdr.js
内容:
/**
* @name placeHolder
* @class 跨浏览器placeHolder,对于不支持原生placeHolder的浏览器,通过value或插入span元素两种方案模拟
* @param {Object} obj 要应用placeHolder的表单元素对象
* @param {Boolean} span 是否采用悬浮的span元素方式来模拟placeHolder,默认值false,默认使用value方式模拟
*/
//执行
jQuery(function(){
jQuery(':input[placeholder]').each(function(index, element) {
placeHolder(element,true);
});
});
function placeHolder(obj, span) {
if (!obj.getAttribute('placeholder')) return;
var imitateMode = span===true?true:false;
var supportPlaceholder = 'placeholder' in document.createElement('input');
if (!supportPlaceholder) {
var defaultValue = obj.getAttribute('placeholder');
var type = obj.getAttribute('type');
if (!imitateMode) {
obj.onfocus = function () {
(obj.value == defaultValue) && (obj.value = '');
obj.style.color = '';
}
obj.onblur = function () {
if (obj.value == defaultValue) {
obj.style.color = '';
} else if (obj.value == '') {
obj.value = defaultValue;
obj.style.color = '#ACA899';
}
}
obj.onblur();
} else {
var placeHolderCont = document.createTextNode(defaultValue);
var oWrapper = document.createElement('span');
oWrapper.style.cssText = 'position:absolute; color:#ACA899; display:inline-block; overflow:hidden;';
oWrapper.className = 'wrap-placeholder';
oWrapper.style.fontFamily = getStyle(obj, 'fontFamily');
oWrapper.style.fontSize = getStyle(obj, 'fontSize');
oWrapper.style.marginLeft = parseInt(getStyle(obj, 'marginLeft')) ? parseInt(getStyle(obj, 'marginLeft')) + 3 + 'px' : 3 + 'px';
oWrapper.style.marginTop = parseInt(getStyle(obj, 'marginTop'))!=0 ? getStyle(obj, 'marginTop'): 0 + 'px';
oWrapper.style.paddingLeft = getStyle(obj, 'paddingLeft');
oWrapper.style.width = (obj.offsetWidth - parseInt((getStyle(obj, 'marginLeft')=="auto"?0:(getStyle(obj, 'marginLeft')))))==0?100:(obj.offsetWidth - parseInt((getStyle(obj, 'marginLeft')=="auto"?0:(getStyle(obj, 'marginLeft'))))) + 'px';
oWrapper.style.height = obj.offsetHeight==0?34:obj.offsetHeight + 'px';
oWrapper.style.lineHeight = obj.nodeName.toLowerCase()=='textarea'? '':(obj.offsetHeight==0?34:obj.offsetHeight) + 'px';
oWrapper.appendChild(placeHolderCont);
obj.parentNode.insertBefore(oWrapper, obj);
oWrapper.onclick = function () {
obj.focus();
};
//绑定input或onpropertychange事件,ie9中删除时无法触发此事件
if (typeof(obj.oninput)=='object') {
obj.addEventListener("input", changeHandler, false);
obj.onpropertychange = changeHandler;
obj.onkeyup = delHandler;
} else {
obj.onpropertychange = changeHandler;
obj.onkeyup = delHandler;
}
function changeHandler() {
oWrapper.style.display = obj.value != '' ? 'none' : 'inline-block';
}
function delHandler(e){//监听del、backspace、ctrl+x
var e = e || window.event;
if(e.keyCode == 8 || e.keyCode == 46 || (event.ctrlKey&&e.keyCode == 88)){
oWrapper.style.display = obj.value != '' ? 'none' : 'inline-block';
}
}
/**
* @name getStyle
* @class 获取样式
* @param {Object} obj 要获取样式的对象
* @param {String} styleName 要获取的样式名
*/
function getStyle(obj, styleName) {
var oStyle = null;
if (obj.currentStyle)
oStyle = obj.currentStyle[styleName];
else if (window.getComputedStyle)
oStyle = window.getComputedStyle(obj, null)[styleName];
return oStyle;
}
}
}
}
使用
<input type="text" name="userName" id="userName" placeholder="用户名"/>
<input type="password" name="userPassWord" id="userPassWord" placeholder="密码" />
JQuery Placeholder - Input提示信息的更多相关文章
- 基于jQuery的input输入框下拉提示层(自动邮箱后缀名)
基于jQuery的input输入框下拉提示层,方便用户输入邮箱时的提示信息,需要的朋友可以参考下 效果图 // JavaScript Document (function($){ $.fn ...
- jquery.placeholder.js的使用
最近做东西用到placeholder这个属性,可是这个属性在低版本的IE或者QQ浏览器等这些浏览器上这个属性不能生效,后来在网上查了下,发现了jquery的一个插件jquery.placeholder ...
- jquery.placeholder.min.js让吃屎的IE浏览器支持placeholder去吧
描述:现在都是HTML5时代了,所有的浏览器都支持placeholder,唯独IE不支持.现在我们有了这款插件,IE下终于可以支持了! 图片展示: 兼容浏览器:IE6+/Firefox/Goog ...
- jQuery弹出提示信息简洁版(自动消失)
之前看了有一些现成的blockUI.Boxy.tipswindow等的jQuery弹出层插件,可是我的要求并不高,只需要在保存后弹出提示信息即可,至于复杂点的弹出层-可以编辑的,我是直接用bootst ...
- 基于MVC4+EasyUI的Web开发框架经验总结(1)-利用jQuery Tags Input 插件显示选择记录
最近花了不少时间在重构和进一步提炼我的Web开发框架上,力求在用户体验和界面设计方面,和Winform开发框架保持一致,而在Web上,我主要采用EasyUI的前端界面处理技术,走MVC的技术路线,在重 ...
- 为什么Jquery对input file控件的onchange事件只生效一次
今天在做jquery对input file控件的onchange事件进行监听,就一直只生效一次,不知道Jquery为什么对file控件没有做到每次改变触发onchange事件的效果,但是还是有好几种解 ...
- JQuery设置input属性(disabled、enabled)
document.getElementById("removeButton").disabled = false; //普通Js写法 $("#removeButton&q ...
- JQuery让input从disabled变成enabled
JQuery让input从disabled变成enabled document.getElementByIdx_x("removeButton").disabled = false ...
- jQuery 操作 input 之 checkbox
jQuery 操作 input 之 checkbox 一片 HTML 清单: <input type="checkbox" name="hobby" va ...
随机推荐
- label for
一般我们在使用radio 时都是结合label来使用的 <label><input type=”radio” name=”radio” value=’0’/>男</l ...
- mysql 配置 utf8 依然乱码
mysql 乱码问题排除方案: 1.检查数据库及数据表是不是utf8字符集 2.查看一下jdbc.properties配置的数据库url 是否配置了characterEncoding=UTF-8或者在 ...
- ajax 异步插入图片到数据库(单图上传)
其实也没啥 如图: 点击按钮选择图片,选择完成后 无需点击确定 ,自动上传到服务器指定文件夹 然后插入到数据库中. 下面来看看这要代码 index.php <!DOCTYPE HTML> ...
- jquery中的cookie操作
使用前在页面中引入下面的代码 /*! * jQuery Cookie Plugin v1.4.1 * https://github.com/carhartl/jquery-cookie * * Cop ...
- PDF 补丁丁 0.4.2.1023 测试版发布:新增旋转页面功能
新的测试版发布啦.此版本增加了旋转页面的功能. 在“PDF文档选项”对话框的“页面设置”选项卡中,可设置需要旋转的页面(输入页码范围),以及旋转角度. 此外,还修复了统一页面尺寸功能的小问题.
- iOS开发多线程篇—GCD的基本使用
iOS开发多线程篇—GCD的基本使用 一.主队列介绍 主队列:是和主线程相关联的队列,主队列是GCD自带的一种特殊的串行队列,放在主队列中得任务,都会放到主线程中执行. 提示:如果把任务放到主队列中进 ...
- windows下上传文件到VWware的centos
1.使用Xsell 连接到centos虚拟机,可以在虚拟机使用终端查看ip地址,命令:ifconfig -a 2.远程Linux系统上需要安装lrzsz工具包 Xsell命令:yum install ...
- Google加强版权保护
在版权保护方面,我们一直是反面教材,而在场面上Google早已退出我们的世界,所以Google的加强版权保护对国内的互联网不会有太多的影响,即便无法在Google搜索到我们需要的XXX软件破解版,百度 ...
- JEECMS v8 发布,java 开源 CMS 系统
JEECMSv8 是国内java开源CMS行业知名度最高.用户量最大的站群管理系统,支持栏目模型.内容模型交叉自定义.以及具备支付和财务结算的内容电商为一体: 对于不懂技术的用户来说,只要通过后台的 ...
- Jmeter—2 http请求—简单的get请求
发送一个简单的get http请求 1 启动Jmeter,在测试计划上点击鼠标右键>添加>Threads(Users)>线程组 2 线程组界面.可设置线程数,几秒启动所有线程,循环次 ...