在前端开发中,经常需要为input设置placeholder属性,但是placeholder是HTML5新属性,在IE10以下不兼容,那么如何完美兼容呢?

网上搜索了一下,其实也挺简单的,可以采用以下方法:

<script type="text/javascript">
if (!('placeholder' in document.createElement('input'))) {
$('input[placeholder],textarea[placeholder]').each(function() {
var that = $(this),
text = that.attr('placeholder');
if (that.val() === "") {
that.val(text).addClass('placeholder');
}
that.focus(function() {
if (that.val() === text) {
that.val("").removeClass('placeholder');
}
}).blur(function() {
if (that.val() === "") {
that.val(text).addClass('placeholder');
}
}).closest('form').submit(function() {
if (that.val() === text) {
that.val('');
}
});
});
}
<script>

  这段代码很容易理解,但是用的时候发现对type=password类型不支持,于是继续搜索直到有了JPlaceHolder.js,感觉很不错,用起来也很简单

<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content=""> </head> <body>
<input type="password" placeholder="密码">
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="JPlaceHolder.js"></script>
</body> </html>

  下载地址:JPlaceHolder.js

IE如何兼容placeholder属性的更多相关文章

  1. html5的placeholder属性(IE如何兼容placeholder属性)

    界面UI推荐 jquery html5 实现placeholder兼容password  IE6 html5的placeholder属性(IE如何兼容placeholder属性) 2013-01-05 ...

  2. 【工作笔记五】html5的placeholder属性(IE如何兼容placeholder属性)

    placeholder属性,IE对其的支持表示无奈,firefox.google chrome表示毫无压力. HTML5对Web Form做了许多增强,比如input新增的type类型.Form Va ...

  3. 低版本IE内核浏览器兼容placeholder属性解决办法

    最简便的一个方法,通过js实现. <input type="text" name="username" id="username" v ...

  4. placeholder属性兼容ie8

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

  5. 兼容placeholder

    众所周知.IE9以下不兼容placeholder属性,因此自己定义了一个jQuery插件placeHolder.js.以下为placeHolder.js的代码: /** * 该控件兼容IE9下面,专门 ...

  6. input 的 placeholder属性在IE8下的兼容处理

    placeholder是input标签的新属性,在使用的时候有两个问题: 1.IE8 下不兼容 处理思路: 如果浏览器不识别placeholder属性,给input添加类名placeholder,模仿 ...

  7. 改进《完美让IE兼容input placeholder属性的jquery实现》的不完美

    <完美让IE兼容input placeholder属性的jquery实现>中的代码在IE9以下浏览器中会出错,原因是因为ie9以下的浏览器对input的标签的解释不同. 例如对以下文本框的 ...

  8. HTML5表单提示placeholder属性兼容IE

    placeholder 属性提供可描述输入字段预期值的提示信息(hint). 该提示会在输入字段为空时显示,并会在字段获得焦点时消失. 注释:placeholder 属性适用于以下的 <inpu ...

  9. 兼容ie10以下版本的placeholder属性

    <script src="${ctx }/js/jquery.placeholder.js" type="text/javascript">< ...

随机推荐

  1. 10.python中的序列

    本来说完字符串.数字.布尔值之后,应该要继续讲元祖.列表之类的.但是元祖和列表都属于序列,所以有必要先讲讲python的序列是什么. 首先,序列是是Python中最基本的数据结构.序列中的每个元素都分 ...

  2. HelloWorld IL代码

    .assembly extern mscorlib .assembly HelloWorld.class HelloWorld extends [mscorlib] System.Object {   ...

  3. 菜鸟学习Spring——60s使用annotation实现简单AOP

    一.概述. AOP大家都知道切面编程,在Spring中annotation可以实现简单的AOP列子.下面还未大家介绍几个概念: Aspect 对横切性关注点的模块化. Advice 对横切性关注点的具 ...

  4. Linux软链接与硬链接

    1.Linux链接概念Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接. [硬连接]硬连接指通过索引节点 ...

  5. outlook配置

    有时打开outlook报错.步骤如下: 一.打开控制面板-邮件-电子邮件账户-新建 二.具体设置如下: 三.点第二步上的“其他设置(M)”.做发送服务器.

  6. ListView的多布局中的小问题

    今天用到了ListView的多布局,我们需要额外重写两个方法 //返回多布局的个数 @Override public int getViewTypeCount() { return 3; } //用该 ...

  7. Reverse String

    Write a function that takes a string as input and returns the string reversed. Example:Given s = &qu ...

  8. Python3.x List方法集合

    Python中的一个内置的基础类型叫List,类比于OC中的Array..他是有序的,那么下面写一些基础的方法,以备需要时候查阅: 1>获取List的长度 len(List) 2> 索引访 ...

  9. MongoDB学习笔记-创建、更新、删除文档

    创建     MongoDB中使用insert方法来向集合插入文档,然后保存到MongoDB中.     db.foo.insert({"hehe":"呵呵"} ...

  10. .net mvc通过ucenter和 discuz的整合,nopcommerce ucenter 插件的方式实现

    discuz无疑是目前市面上最好的论坛之一,对于大多数公司来说,用discuz搭建一个论坛肯定是最节约成本的选择,然而我们的会员想要和discuz进行整合的话,只有两种荀泽,一种直接选用discuz的 ...