在IE8等不支持placeholder属性的浏览器中模拟placeholder效果
placeholder是一个很有用的属性,可以提示用户在input框中输入正确的内容,但是IE8以及IE8一下的浏览器不支持该属性,我们可以使用js来模拟相似的效果。下面直接上代码:
<!doctype html>
<html>
<header>
<meta charset="utf-8">
<title>placeholder</title>
<style type="text/css">
*{margin:0;padding:0;}
form {margin:50px;}
input {display:block;margin:0 0 6px 0;}
</style>
</header>
<body>
<form>
用户名:<input name="name" placeholder="请输入用户名"/>
密码:<input name="password" placeholder="请输入登陆密码"/>
</form>
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(function(){
function placeholderSupport() { // 判断浏览器是否支持 placeholder
return 'placeholder' in document.createElement('input');
} if(!placeholderSupport()){
$(document).on('focus', '[placeholder]',function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
input.removeClass('placeholder');
}
});
$(document).on('blur', '[placeholder]',function() {
var input = $(this);
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('placeholder');
input.val(input.attr('placeholder'));
}
});
$('[placeholder]').blur();
};
});
</script>
</body>
</html>
'placeholder' in document.createElement('input') ,该行代码利用 in 表达式来判断浏览器中的 input 元素中是否有 placeholder 属性存在。
$('[placeholder]') 是一个Jquery 的属性选择器,选择页面上所有的具有 placheholder 属性的元素。在页面加载完成时我们首先调用 blur() 方法,将所有 input 元素的值 设置 为 input 属性 placeholder 对应的值,从而模拟 placeholder 的效果。
效果如下图所示:

选中 input 时的效果:

在IE8等不支持placeholder属性的浏览器中模拟placeholder效果的更多相关文章
- html5的placeholder属性(IE如何兼容placeholder属性)
界面UI推荐 jquery html5 实现placeholder兼容password IE6 html5的placeholder属性(IE如何兼容placeholder属性) 2013-01-05 ...
- onunload事件火狐不支持,在IE浏览器中,只有刷新时该事件才发生
onunload事件火狐不支持,在IE浏览器中,只有刷新时该事件才发生
- 解决seajs ie8 对象不支持charAt 属性。
在使用 seajs做项目,今天偶然发现在ie9以下的ie版本会 报出 对象不支持charAt 属性.刚开始还以为是自己写的js部分出了问题,经过几个小时的奋战.最终找到了其根源.在sea-debug. ...
- 【工作笔记五】html5的placeholder属性(IE如何兼容placeholder属性)
placeholder属性,IE对其的支持表示无奈,firefox.google chrome表示毫无压力. HTML5对Web Form做了许多增强,比如input新增的type类型.Form Va ...
- 给IE9及其以下等不支持classList属性的浏览器,添加classList属性
// 解决IE9及其以下 不支持classList属性的问题 if (!("classList" in document.documentElement)) { Object.de ...
- 兼容的placeholder属性
作为一个.net后台开发的程序猿,博客里既然大多都是前端相关的博文.是不是该考虑换方向了,转前端开发得了 ... 小小吐槽一下,近期受该不该跳槽所困惑,我有选择困难症! 继续前端,这次说一下输入框 p ...
- HTML5表单提示placeholder属性兼容IE
placeholder 属性提供可描述输入字段预期值的提示信息(hint). 该提示会在输入字段为空时显示,并会在字段获得焦点时消失. 注释:placeholder 属性适用于以下的 <inpu ...
- HTML 5 <input> placeholder 属性
原文链接:http://www.w3school.com.cn/html5/att_input_placeholder.asp HTML 5 <input> placeholder 属性 ...
- Hmtl5 <input>中placeholder属性(新属性)
Hmtl5 <input>中placeholder属性(新属性) 一.定义和用法 placeholder 属性提供可描述输入字段预期值的提示信息(hint). 该提示会在输入字段为空时显示 ...
随机推荐
- 还在抱怨JS文件里没有智能提示吗, VS10以及以上都可以 .NET
1.打开JS文件 2.编写$.我们会发现什么也没有 3.托进来 4.有了哈 保存头部代码新建JS都贴上去.
- 五分钟,运用cocoaui库,搭建主流iOS app中我的界面
本项目基于天天团购项目,在上一篇中有说到! 首先介绍一些cocoaui,是国内的一名程序员做的开源的开源系统,目的是为了简化ios布局!官网地址:www.cocoaui.com,github地址:ht ...
- 我的WCF学习与强化系列文章
1.WCF服务创建与使用(请求应答模式) 2.WCF服务创建与使用(双工模式) 3.WCF序列化 4.WCF实现方法重载 5.WCF会话(Session)与实例(Instance)管理 后面会继续更新 ...
- 设计模式--原型(Prototype)模式
写这些也许有人认为“为了模式而模式”.Insus.NET所想到的,每个大师成为大师之前,也许都得这样做. 走路,从小就开始学,直至现在,谁还不是为了走路而走路?一直重复着...... 很多人没有分享自 ...
- asp.net的code-Behind技术
新建一个VS.NET下的项目..看到ASPX,RESX和CS三个后缀的文件了吗??这个就是代码分离.实现了HTML代码和服务器代码分离.方便代码编写和整理. code-Behind:asp.net中的 ...
- Python入门笔记(13):列表解析
一.列表解析 列表解析来自函数式编程语言(haskell),语法如下: [expr for iter_var in iterable] [expr for iter_var in iterable i ...
- SQL Server中@@ROWCOUNT的用法
SQL Server中@@ROWCOUNT返回受上一语句影响的行数,返回值类型为 int 整型. 如果行数大于 20 亿,则需要使用 ROWCOUNT_BIG. @@ROWCOUNT和@@ERROR变 ...
- csharp: Importing or Exporting Data from Worksheets using aspose cell
/// <summary> /// 涂聚文 /// 20150728 /// EXCEL win7 32位,64位OK /// </summary> public class ...
- JPHP试用笔记
JPHP试用指南 编译 环境准备 有JDK 1.6 的环境 Gradle 1.4 以上 具体配置略过,git签出https://github.com/dim-s/jphp/代码后,看readme.md ...
- vmware screen
1. Question Description: the screen of the vmware looks small . 2. Solution: 2.1 look the size of sc ...