placeholder是input标签的新属性,在使用的时候有两个问题:

1、IE8 下不兼容

处理思路:

如果浏览器不识别placeholder属性,给input添加类名placeholder,模仿placeholder属性的样式,并给input 的value赋值placeholder属性的值

2、 input的type属性是password的情况,用上面的方法处理提示语为密码文

处理思路:

新添加一个标签,模仿placeholder属性

直接上代码:

css部分:

  .input-item {
position: relative;
margin: 10px;
}
.pwd-place {
position: absolute;
top:;
left: 72px;
width: 100%;
height: 100%;
font-size: 12px;
}

html部分:

  <form action="#">
<div class="input-item">
<label for="userName">用户名:</label>
<input class="username" id="userName" type="text" placeholder="请输入用户名">
</div>
<div class="input-item">
<label for="pwd">密码:</label>
<input class=" password" id="pwd" type="password" placeholder="请输入密码">
<span class="pwd-place"></span>
</div> </form>

js部分:

 <script src="jquery-1.11.3.js"></script>
<script>
function placeholder(el){ function isPlaceholer(){
var input = document.createElement("input");
return "placeholder" in input;
} var $el = $(el);
if( isPlaceholer()==false && !('placeholder' in document.createElement('input')) ){ $('input[placeholder],textarea[placeholder]').each(function(){
var that = $(this),
text= that.attr('placeholder');
if(that.val()===""){
if(that.attr("type") == "password"){
$el.html("请输入密码");
}else {
that.val(text).addClass('placeholder');
}
}
that.focus(function(){
if($el.html() == text){
$el.html("");
}
if(that.val()===text) {
that.val("").removeClass('placeholder'); }
})
.blur(function(){
if(that.val()==="") {
if (that.attr("type") == "password") {
$el.html("请输入密码"); }else {
that.val(text).addClass('placeholder');
}
}
})
.closest('form').submit(function(){
if(that.val() === text){
that.val('');
}
});
});
}
}
$(document).ready(function() {
placeholder(".pwd-place")
});
</script>

input 的 placeholder属性在IE8下的兼容处理的更多相关文章

  1. input输入框的border-radius属性在IE8下的完美兼容

    在工作中我们发现搜索框大部分都是有圆角的,为此作为经验不足的前端人员很容易就想到,给input标签添加border-radius属性不就解决了嘛.不错方法确实是这样,但是不要忘了border-radi ...

  2. Hmtl5 <input>中placeholder属性(新属性)

    Hmtl5 <input>中placeholder属性(新属性) 一.定义和用法 placeholder 属性提供可描述输入字段预期值的提示信息(hint). 该提示会在输入字段为空时显示 ...

  3. 解决input标签placeholder属性浏览器兼容性问题的一种方法

    为文本框input添加文字输入提示,H5为input提供了一个placeholder属性.在支持H5的浏览器中,用此属性设置输入提示,简单方便,但是对于IE8以下版本,都不支持placeholder属 ...

  4. 修改 input / textarea placeholder 属性的颜色和字体大小

    话不多说,直接上代码: input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { color: #666; fon ...

  5. 前端 html input标签 placeholder属性 标签上显示内容

    前端 html  input标签 的placeholder属性  标签上显示内容 <!DOCTYPE html> <html lang="en"> < ...

  6. IE8下的兼容小经验

    placeholder IE8下不支持HTML5属性placeholder,不过为解决此问题的js插件挺多的,比如:jquery-placeholder.也可以使用jquery来写. last-chi ...

  7. bootstrap在ie8下,兼容媒体查询

    最近使用bootstrap做网站的时候发现,在ie8下的媒体查询一直失效: 后来解决了,做如下记录: 1.必须运行在服务器下 2.hack 条件语法,如下: <!--[if lte ie 9]& ...

  8. ie8不兼容input的placeholder属性但是要实现其效果的方法

    通过学习前辈的思想,个人想法整理如下: 通过两个元素标签,仿造出placeholder的内容 使用position定位好两个元素标签 第一要隐藏两个元素标签display:none,在<!--[ ...

  9. angular自定义指令解决IE89不支持input的placeholder属性

    下面代码实测通过,直接copy到本地运行即可. <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

随机推荐

  1. NuGet -- 使用控制台管理程序包

    为什么要使用控制台管理程序包而不使用程序包管理窗口?原因大家都懂,生活压力这么大,一切都只是为了装一波.开个玩笑,当然不只是此原因,在有些情况下,有些操作使用程序包管理窗口不能达到目的,只能使用控制台 ...

  2. 【转】关于usr/bin/ld: cannot find -lxxx问题总结

    原文网址:http://eminzhang.blog.51cto.com/5292425/1285705 /usr/bin/ld: cannot find -lxxx问题总结   linux下编译应用 ...

  3. Android Activity管理类

    public class AppManager { private static Stack<Activity> activityStack; private static AppMana ...

  4. BZOJ2818: Gcd 莫比乌斯反演

    分析:筛素数,然后枚举,莫比乌斯反演,然后关键就是分块加速(分块加速在上一篇文章) #include<cstdio> #include<cstring> #include< ...

  5. [liu yanling]软件测试的过程

    测试过程按4个步骤进行,即单元测试.组装测试.确认测试和系统测试.

  6. OpenGL ES 的三种变量类型(uniform,attribute和varying)

    1.uniform变量 uniform变量是外部application程序传递给(vertex和fragment)shader的变量.因此它是application通过函数glUniform**()函 ...

  7. 【原】SparkContex源码解读(一)

    版权声明:本文为原创文章,未经允许不得转载. SparkContext(简称sc)是Spark程序的主入口,代表一个连接到Spark集群(Standalone.YARN.Mesos三种集群部署模式)的 ...

  8. 归并排序 求逆序数 链表的归并排序 多线程归并排序 java

    import java.util.Scanner; public class Main { private static int count=0; public static void mergeso ...

  9. iOS开发——View的autoresizingMask属性

    View的自适应属性autoresizingMask属性 每一个UIView都有一个autoresizingMask属性,这个属性是用于适应父视图的大小与子视图适应的,源码如下 enum { UIVi ...

  10. POJ 3279 Fliptile (质量不错)

    题意:有一个M*N的棋盘,每一个格子只有两种状态0或1,每次可以选择一个格子执行翻转操作,并且与该格子相邻的4个格子都会被翻转,求将所有格子都翻转成0所需要的最小操作数,若有多种方案,输出字典序最小的 ...