基于jQuery,实现ie placeholder的兼容性
来源:互联网 发布:新人怎么成为网络作家 编辑:程序博客网 时间:2024/06/05 14:13
placeholder是HTML5<input>的属性之一,在不同的浏览器( 支持HTML5的现代浏览器 )中会有略微不同的显示效果:
实现代码:
CSS:
.phcolor{ color:#999;}
$(function(){ //判断浏览器是否支持placeholder属性 supportPlaceholder='placeholder'in document.createElement('input'), placeholder=function(input){ var text = input.attr('placeholder'), defaultValue = input.defaultValue; if(!defaultValue){ input.val(text).addClass("phcolor"); } input.focus(function(){ if(input.val() == text){ $(this).val(""); } }); input.blur(function(){ if(input.val() == ""){ $(this).val(text).addClass("phcolor"); } }); //输入的字符不为灰色 input.keydown(function(){ $(this).removeClass("phcolor"); }); }; //当浏览器不支持placeholder属性时,调用placeholder函数 if(!supportPlaceholder){ $('input').each(function(){ text = $(this).attr("placeholder"); if($(this).attr("type") == "text"){ placeholder($(this)); } }); } });
$.extend($.fn, { mask: function (content) { var a = this[0]; if (!a) return console.log("mask", "the cDom object is empty"), this; this.unmask(); var b = {}; b.cssText = a.style.cssText; b.nextSibling = a.nextSibling; b.parentNode = a.parentNode; a.style.position = "absolute"; a.style.display = "block"; // var _ina = document.createElement("container"); // _ina.style.cssText = "position:absolute;top:0;left:0;width:0;height:0;z-index:100;"; // var _inb = document.body; // _inb || document.write('<span id="__body__" style="display:none;">cQuery</span>'); // ((_inb = document.body.firstChild) ? document.body.insertBefore(_ina, _inb) : document.body.appendChild(_ina)); // (_inb = document.getElementById("__body__")) && _inb.parentNode.removeChild(_inb); // var _container = $(_ina); // _container.append(a); a.style.left = (document.documentElement.scrollLeft || document.body.scrollLeft || 0) + Math.max(0, (document.documentElement.clientWidth - a.offsetWidth) / 2) + "px"; a.style.top = (document.documentElement.scrollTop || document.body.scrollTop || 0) + Math.max(0, (document.documentElement.clientHeight - a.offsetHeight) / 2) + "px"; var c = "background:#fff;position:absolute;left:0;top:0;width:" + Math.max(document.documentElement.clientWidth, document.documentElement.scrollWidth, document.body.clientWidth, document.body.scrollWidth) + "px;height:" + Math.max(document.documentElement.clientHeight, document.documentElement.scrollHeight, document.body.clientHeight, document.body.scrollHeight) + "px;"; b.maskDiv = document.createElement("div"); b.maskDiv.style.cssText = c + "filter:progid:DXImageTransform.Microsoft.Alpha(opacity=50);opacity:0.5;"; $(b.maskDiv).insertBefore(a); var isIE = /msie/.test(navigator.userAgent.toLowerCase()); isIE && (b.maskIframe = document.createElement("iframe"), b.maskIframe.style.cssText = c + "filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);opacity:0;", $(b.maskIframe).insertBefore(b.maskDiv)); this.data("__mask__", b); return this; }, unmask: function () { if (!this[0]) return console.log("mask", "the cDom object is empty"), this; var a = this.data("__mask__"); a && (this[0].style.cssText = a.cssText, (a.nextSibling ? this.first().insertBefore(a.nextSibling) : this.first().appendTo(a.parentNode)), $(a.maskDiv).remove(), a.maskIframe && $(a.maskIframe).remove(), this.removeData("__mask__")); }, placeholder: function () { if ("placeholder" in document.createElement("input")) { return this; //如果原生支持placeholder属性,则返回对象本身 } else { return this.each(function () { var _this = $(this); _this.focus(function () { if (_this.val() === _this.attr("placeholder")) { _this.css("color", ""); _this.val("") } }).blur(function () { if (_this.val().length === 0) { _this.val(_this.attr("placeholder")); _this.css("color", "gray"); } }); if (!_this.val()) { _this.val(_this.attr("placeholder")); _this.css("color", "gray"); }; }) } }});
0 0
- 基于jQuery,实现ie placeholder的兼容性
- IE下实现placeholder效果的jquery插件
- jQuery实现IE浏览器兼容placeholder效果
- 基于jQuery的placeholder插件
- 两种方法基于jQuery实现IE浏览器兼容placeholder效果【转】
- 两种方法基于jQuery实现IE浏览器兼容placeholder效果
- 两种方法基于jQuery实现IE浏览器兼容placeholder效果
- Jquery解决'placeholder'针对IE的兼容性问题
- 实现ie浏览器对placeholder的兼容
- IE兼容实现HTML5的placeholder
- 简单实现IE9及以下对placeholder的兼容性
- 如何解决placeholder的兼容性
- 【jQuery】使得IE的输入框完成placeholder的使命
- 兼容IE的placeholder
- placeholder的ie兼容性问题
- 用jquery实现html5的placeholder功能
- IE兼容性的问题
- ie的兼容性模式
- 实习三月记:产品经理做什么
- POJ 计算几何入门题目推荐
- 个性化标签系统--架构设计教程
- ORA-01861: 文字与格式字符串不匹配
- oracle 判断一个表是否已存在
- 基于jQuery,实现ie placeholder的兼容性
- 【整理】adb命令、adb shell与Linux各种命令(busybox)
- oracle的导入和导出
- Linux内核最新的连续内存分配器(CMA)——避免预留大块内存
- 启动activity传递参数的另外一种工具类
- AFNetworking 的大体思路
- 还是记一下吧 rockchip android usb
- printf 和scanf的%*s, %*d的作用
- genymotion输入中文