placeholder的兼容处理方法
placeholder是html5新增的一个属性,极大的减轻了表单提示功能的实现,但是对于IE6-IE9真的是只能靠自己写啦!
但是在自己写时会掉进了一个坑里,还好用了一会时间还是爬出来啦。
最终的解决方法方法如下:
<form name="doluform" id="loginform">
<div class="inputdivin">
<input type="text" placeholder="用户名" name="usernamez" value=""/>
<p class="tipsdiv"><span id="logintipsp"></span></p>
</div>
<div class="inputdivin">
<input type="password" placeholder="密码" name="passwordz" value=""/>
<span style="display:none" class="placespan"></span>
<p class="tipsdiv"><span id="loginposswordtipsp"></span></p>
</div>
<div class="inputivin">
<p id="jhemail"></p>
</div>
<div class="inputdivin">
<button type="submit" title="点击登录" class="submitbtn">登 录</button>
</div>
<div class="inputdivin">
<p class="passwordbc"><a href="javascript:void(0);" class="passwordaction" id="holdpassword"><b class="test"></b> 记住密码<input type="hidden" name="remfor_input" value="1"/></a></p>
<p class="findpassword"><a href="${ctx}/home/index/findpassword_step1.jsp" class="passwordaction">忘记密码</a></p>
</div>
</form>
而对应的js组件写法如下:
var Inputplace=function(){
this.obj=null;
this.type="";
this.color="";
this.tipspan=null;
}
Inputplace.prototype.init=function(obj,opt){
var This=this;
this.obj=obj;
this.setting={
"defaultz":obj.attr("placeholder"),
"tccolor":"#6d696a"
}
$.extend(this.setting,opt);
this.oldcolor=obj.css("color");
this.type=obj.attr("type");
if(this.type=="text"){//文本input设置
this.obj.val(this.setting.defaultz).css("color",this.setting.tccolor);
this.obj.unbind("focus");
this.obj.focus(function(){
if(This.obj.val()==This.setting.defaultz){
This.obj.val("").css("color",This.oldcolor);
}
})
this.obj.unbind("blur");
this.obj.blur(function(){
if(This.obj.val()=="" || /^\s*$/.test(This.obj.val())){
This.obj.val(This.setting.defaultz).css("color",This.setting.tccolor);
}
})
}
else if(this.type=="password"){//密码input实现
this.tipspan=this.obj.next("span.placespan");
this.tipspan.show().html(this.setting.defaultz).css({"color":this.setting.tccolor});
this.obj.unbind("focus");
this.obj.focus(function(){
This.tipspan.hide();
})
this.tipspan.unbind("click");
this.tipspan.click(function(){
$(this).hide();
This.obj.focus();
})
this.obj.unbind("blur");
this.obj.blur(function(){
if(This.obj.val()=="" || /^\s$/.test(This.obj.val())){
This.tipspan.show();
}
})
}
}
而调用方法如下:
<!--[if lte IE 9 ]>
<script type="text/javascript" src="${ctx}/static/js/common/jsplaceholder.js"></script>
<script type="text/javascript">
$(function(){
var inputtext=$("input:text");
inputtext.each(function(){
new Inputplace().init($(this))
})
var inputpass=$("input:password");
inputpass.each(function(){
new Inputplace().init($(this))
})
})
</script>
<![endif]-->
其中主要的坑就是在于input的类型上,当input为password的时候,如果你还只是直接设置val来做提示,那你就已经掉坑里啦,因为在password 会把输入的内容成..的形式,所以得绕一下,如果是password的话可以在password的那一组里新增一个元素去显示要提示的内容,像其中<span style="display:none" class="placespan"></span>就是专门用来做提示用的,在CSS里要先写好提示所用到的一干样式,样式为tipsdiv的P元素是用来放验证提示内容的,这里不用管,当表单获得焦点的时候或者span被点击的时候span都会消失,而input开始可以输入内容啦。
<div class="inputdivin">
<input type="password" placeholder="密码" name="registerpassword" value=""/>
<span style="display:none" class="placespan"></span>
<p class="tipsdiv"><span id="registerpasswordtipsid"></span></p>
</div>
注:就在html5的placeholder能用的情况下,在各浏览器下也会有一些差异,在ff和chrome下,输入框获得焦点时,placeholder文字没有变化,只有当输入框中输入了内容时,placeholder文字才消失;而在safari和ie10-ie11下,当输入框获得焦点时,placeholder文字便立即消失。
默认情况下,placeholder的文字是灰色,输入的文字是黑色。而我们拿到的设计稿上的色值往往并不与默认情况下完全一致。那么,如何修改placeholder的色值呢?
如果直接写input{color:red;},可以看到,ie10和ff下,placeholder文字和输入文字都变成了红色.
而在chrome和safari下,placeholder文字颜色不变,只有输入的文字变成红色。
placeholder的兼容处理方法的更多相关文章
- placeholder不兼容 IE10 以下版本的解决方法
对于密码输入框placeholder的兼容问题:HTML代码:<input type="password" id="loginPassword" plac ...
- IE6-7下margin-bottom不兼容解决方法(非原创,视频中看到的)
在IE低版本下有很多不兼容,现在将看到的 IE6-7下margin-bottom不兼容解决方法 演示一下,方便日后自己查阅. <!DOCTYPE html> <html la ...
- 兼容ie浏览器的placeholder的几种方法
项目中遇到的问题,试了几种方法,今天整理出来,如果有不合适的地方,希望大家多多提意见. 第一种方法是:使用html新增的属性 “data-”来实现的,实现的时候,input框没有使用placehole ...
- (转)html5 Placeholder属性兼容IE6、7方法
使低版本浏览器支持Placeholder有很多方法,都不是很完美,或多或少有点问题,且有些原生支持的浏览器在获得焦点时会清空Placeholder提示.发现zhihu的解决方法不错,特记录下 wind ...
- ie9 placeholder兼容代码方法
function guid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r ...
- placeholder解决兼容各种IE浏览器的方法
<input id="search" type="text" class="box" class="inputText&qu ...
- placeholder的兼容处理(jQuery下)
这是一个老问题,结合前辈们的经验,需要处理的问题有一下几个. 1.只有输入框(input/textarea)下的palaceholder属性存在的时候才需要处理这类兼容 2.处理好输入框上焦点和是焦点 ...
- placeholder属性兼容ie8
<!doctype html> <html> <head> <meta charset="utf-8" /> <title&g ...
- html5 placeholder属性兼容ie11
placeholder 属性是html5的属性,用于提供描述输入字段预期值的提示信息(hint). 简单例子: <!DOCTYPE HTML> <html> <body& ...
随机推荐
- [cf2015ICLFinalsDiv1J]Ceizenpok’s formula
题意:$C_n^m\% k$ 解题关键:扩展lucas+中国剩余定理裸题 #include<algorithm> #include<iostream> #include< ...
- 关于WPF的弹出窗口
几个重要的概念需要清楚: Show和ShowDialog区别 1.调用Show方法后弹出子窗口后,线程会继续往下执行.调用ShowDialog方法弹出子窗口后,线程会阻塞,直到子窗口关闭才继续往下执行 ...
- François Hollande’s&…
EVER since President François Hollande was elected last May, things have not gone right for him. He ...
- day6 面向对象(2)
static关键字 1:如果没有static会怎样? 1:定义Person类 1:姓名.年龄.国籍,说话行为 2:多个构造,重载形式体现 2:中国人的国籍都是确定的 1:国籍可以进行显示初始化 cla ...
- 「一入 Java 深似海 」系列课程
第一期 「一入 Java 深似海 」系列课程 - 第一期 第一节:Java 语言基础
- 多列组合为主键(PRIMARY KEY)
在表中,想把其中2列或多列作为组合主键. CREATE TABLE [dbo].[T3] ( ) NOT NULL, ) NOT NULL, ) NULL, ) NULL ) GO ALTER TAB ...
- 从头开始学Web开发—CSS_01
CSS导入使用及引用的两种方法: 外部引用 外部引用的方式,我们直接可以通过link标签来引用我们写好的一个CSS文件: 在link 标签中,我们通过rel = "stylesheet&qu ...
- Vue Study [1]: Vue Setup
Description The article for vue.js Setup. Original post link:https://www.cnblogs.com/markjiang7m2/p/ ...
- Unite 2017 干货整理 同步篇
http://www.kisence.com/2017/05/17/unite-2017-gan-huo-zheng-li-tong-bu-pian/ Unite 2017 干货整理 同步篇 2017 ...
- tarjan求割点割边的思考
这个文章的思路是按照这里来的.这里讨论的都是无向图.应该有向图也差不多. 1.如何求割点 首先来看求割点.割点必须满足去掉其以后,图被分割.tarjan算法考虑了两个: 根节点如果有两颗及以上子树,它 ...