IE兼容-placeholder的方法
来源:互联网 发布:泉州淘宝美工速成班 编辑:程序博客网 时间:2024/05/16 05:36
由于IE8以下的浏览器不支持placeholder。
然后通常解决浏览器兼容的问题都是两种思路嘛,一种就是平滑下降,去掉功能。另一种就是写一个(或者找一个)实现这个功能的库。
而因为交互的原因,使用placeholder后,如果删去,对网站的功能会造成很大的影响,所以也只好用写库代替的方法了。
一、首先是网上摘录的一个自动填写value的方法
$(function(){ handlePlaceholderForIE();});//原始从网上摘录的方法 function handlePlaceholderForIE() { // placeholder attribute for ie7 & ie8 if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) <= 9) { // ie7&ie8 jQuery('input[placeholder], textarea[placeholder]').each(function () { var input = jQuery(this); jQuery(input).val(input.attr('placeholder')); jQuery(input).focus(function () { if (input.val() == input.attr('placeholder')) { input.val(''); } }); jQuery(input).blur(function () { if (input.val() == '' || input.val() == input.attr('placeholder')) { input.val(input.attr('placeholder')); } }); }); } }
然后这个方法会有缺陷,就是如果input是密码的话,则填写上去的内容会是两个小黑点
二、知乎的方法
记忆中,知乎的方法是在placeholder附近加上一个带文字的label,然后在密码的input中,使用label覆盖上去,代替value文字。根据这个思路,修改代码为:
//修改版后function handlePlaceholderForIE() { // placeholder attribute for ie7 & ie8 if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) <= 9) { // ie7&ie8 jQuery('input[placeholder], textarea[placeholder]').each(function () { var input = jQuery(this); if(input.attr("type") == "password"){ $("#txtPwd_l").show() }else{ jQuery(input).val(input.attr('placeholder')); } jQuery(input).focus(function () { if(input.attr("type") == "password"){ $("#txtPwd_l").hide(); } if (input.val() == input.attr('placeholder')) { input.val(''); } }); jQuery(input).blur(function () { if (input.val() == '' || input.val() == input.attr('placeholder')) { if(input.attr("type") == "password"){ $("#txtPwd_l").show() }else{ input.val(input.attr('placeholder')); } } }); }); } }//<input class="fxnloginnew" id="prePwd" style="background:#fff\0;" type="password" value="" placeholder="密码"><label id="txtPwd_l" style="display:none; position: absolute;top: 35px; left: 10px;color:#898989;">密码</label>
三、最终方法
然而为了使用方便,觉得可以写一个js,直接依靠js来生成label,这样就可以更加傻瓜的使用这个函数了。
//因此最终觉得可以写一个JS的插件
//然而因为我懒,还没有写出来,哈哈哈哈哈哈
//by 2015.8.12
0 0
- IE兼容-placeholder的方法
- 兼容IE的placeholder
- Html5的placeholder属性(IE兼容)
- placeholder在ie浏览器的兼容
- 实现ie浏览器对placeholder的兼容
- IE兼容实现HTML5的placeholder
- IE不兼容placeholder的解决方案
- html5的placeholder属性(IE如何兼容placeholder属性)
- placeholder兼容ie
- placeholder 兼容IE
- placeholder兼容ie
- IE兼容html5的placeholder属性(IE如何兼容placeholder属性)
- 使IE兼容placeholder属性
- html5 placeholder ie版本兼容
- html5兼容IE placeholder问题
- placeholder兼容ie和password
- 文本框 placeholder 兼容IE7 IE 8不能显示的效果
- Html5 placeholder 兼容(不支持的浏览器ie)
- 计算机网络之面试常考
- 【ODPS】利用阿里云ODPS作业进行圆周率Pi的计算
- 找工作啦。。。。
- ListView的使用方法
- 【那些年我们不得不用的流氓软件】
- IE兼容-placeholder的方法
- 常量折叠
- 虚拟内存与地址转换
- 基于容器的微服务架构剖析
- Linux中使用xmanager安装oracle数据库
- HD1863 畅通工程 【Kruskal】
- android animation
- HTTP协议详解(真的很经典)
- POJ2773 Happy 2006【容斥原理】