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">登&nbsp;录</button>
</div>
<div class="inputdivin">
<p class="passwordbc"><a href="javascript:void(0);" class="passwordaction" id="holdpassword"><b class="test"></b>&nbsp;&nbsp;记住密码<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文字的颜色,并不想改变输入文字的颜色。
正确的写法如下:
    ::-moz-placeholder{color:red;}              //ff
    ::-webkit-input-placeholder{color:red;}     //chrome,safari
    :-ms-input-placeholder{color:red;}          //ie10

placeholder的兼容处理方法的更多相关文章

  1. placeholder不兼容 IE10 以下版本的解决方法

    对于密码输入框placeholder的兼容问题:HTML代码:<input type="password" id="loginPassword" plac ...

  2. IE6-7下margin-bottom不兼容解决方法(非原创,视频中看到的)

    在IE低版本下有很多不兼容,现在将看到的   IE6-7下margin-bottom不兼容解决方法   演示一下,方便日后自己查阅. <!DOCTYPE html> <html la ...

  3. 兼容ie浏览器的placeholder的几种方法

    项目中遇到的问题,试了几种方法,今天整理出来,如果有不合适的地方,希望大家多多提意见. 第一种方法是:使用html新增的属性 “data-”来实现的,实现的时候,input框没有使用placehole ...

  4. (转)html5 Placeholder属性兼容IE6、7方法

    使低版本浏览器支持Placeholder有很多方法,都不是很完美,或多或少有点问题,且有些原生支持的浏览器在获得焦点时会清空Placeholder提示.发现zhihu的解决方法不错,特记录下 wind ...

  5. ie9 placeholder兼容代码方法

    function guid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r ...

  6. placeholder解决兼容各种IE浏览器的方法

    <input id="search" type="text" class="box" class="inputText&qu ...

  7. placeholder的兼容处理(jQuery下)

    这是一个老问题,结合前辈们的经验,需要处理的问题有一下几个. 1.只有输入框(input/textarea)下的palaceholder属性存在的时候才需要处理这类兼容 2.处理好输入框上焦点和是焦点 ...

  8. placeholder属性兼容ie8

    <!doctype html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. html5 placeholder属性兼容ie11

    placeholder 属性是html5的属性,用于提供描述输入字段预期值的提示信息(hint). 简单例子: <!DOCTYPE HTML> <html> <body& ...

随机推荐

  1. 关于Socket 多线程 的一篇好文章

    http://www.kegel.com/c10k.html#topIt's time for web servers to handle ten thousand clients simultane ...

  2. Jasper:SAOP API 函数

    ylbtech-Jasper:SAOP API 函数 1.设备API返回顶部 1. 设备 设备 API 可以访问详细的设备(SIM 卡)信息,包括当前会话.您也可以更改属性值. API 调用 描述 A ...

  3. PDF,IMAGE,HTML,WORD,EXCEL 互操作

    http://www.cnblogs.com/lyl6796910/p/3318056.html

  4. QDUOJ 河老师的新年礼物(尺取法)

    河老师的新年礼物 发布时间: 2017年1月1日 15:11   最后更新: 2017年1月1日 15:13   时间限制: 1000ms   内存限制: 256M 描述 河老师的新年礼物是一个长度为 ...

  5. 关于 == 和 equals() 的区别

    对于正在学习java的,以及入行不久的小伙伴们,在面试中经常会被面试官问到 "  == 和 equals() 的区别 ?"的问题,你是否回答好了呢? 示例一: //两个基本类型数据 ...

  6. Django 开发拓展 auth 模块,注册用户时发生 ValueError: The given username must be set

    原因 使用局部钩子函数 _clean_fields() 对 username.email 字段进行验证时,未返回,具体请参考 _clean_fields() 函数源码. def clean_usern ...

  7. JAVA基础--IO输入输出(File使用)17

    一. File中的方法 1.  列举方法 /* * 获取指定目录以及子目录下的所有文件和文件夹 */ public class ListFilesDemo { public static void m ...

  8. PostgreSQL 务实应用(五/5)常用表达

    在实际应用中,对于具体的数据计算我们会找相应的函数来实现.而计算需求不同的表达,往往会使得我们使用不同的函数或方式来实现.或者也可以说,同一计算可以使用多种不同的表达方式实现. PostgreSQL ...

  9. matplotlib画线(2)

    这篇随笔是matplotlib画线的补充>>> #nocl参数控制图例中有几列,>>> import numpy as np>>> import ...

  10. gulp --watch直接退出,并没有监听

    1.在es6(彩票项目)搭建环境时遇到gulp --watch 只运行一次就退出了不能监听: D:\nodejs\es6-base>gulp --watch [::] Failed to loa ...