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

windows系统中以下浏览器测试通过:Google Chrome 18,Firefox 12,IE 9,IE 6,Opera 11.5。在android 4.0系统也测试了下,原生浏览器及Opera Mobile12基本通过,缺陷是获得焦点时会清空Placeholder提示。

jquery.placeholder.js部分:

 /*
* html5 placeholder pollfill
* - 使用绝对定位内嵌层
* - 也适用于密码域
* 目标浏览器: IE 6~9, FF 3.5
```
// 默认
$('input[placeholder]').placeholder() // autofocus 与 placeholder 搭配时,非 webkit 清空了提示文本,推荐
$('input[placeholder]').placeholder({
// 将删除原有 placehodler 属性,强制用 JS 实现替代
useNative: false,
// focus 时不清除提示文本, keypress 有效字符时才清空
hideOnFocus: false,
// 附加样式
style: {
textShadow: 'none'
}
})
```
*/
(function ($) {
var attr = 'placeholder', nativeSupported = attr in document.createElement('input') $.fn.placeholder = function (options) {
return this.each(function () {
var $input = $(this) if ( typeof options === 'string' ) {
options = { text: options }
} var opt = $.extend({
text : '',
style : {},
namespace: 'placeholder',
useNative: true,
hideOnFocus: true
}, options || {}) if ( !opt.text ) {
opt.text = $input.attr(attr)
} if (!opt.useNative) {
$input.removeAttr(attr)
}else if ( nativeSupported ) {
// 仅改变文本
$input.attr(attr, opt.text)
return
} var width = $input.width(), height = $input.height()
var box_style = ['marginTop', 'marginLeft', 'paddingTop', 'paddingLeft', 'paddingRight'] var show = function () { $layer.show() }
var hide = function () { $layer.hide() }
var is_empty = function () { return !$input.val() }
var check = function () { is_empty() ? show() : hide() } var position = function () {
var pos = $input.position()
if (!opt.hideOnFocus) {
// 按??藏的情况,需要移?庸?肆较袼
pos.left += 2
}
$layer.css(pos)
$.each(box_style, function (i, name) {
$layer.css(name, $input.css(name))
})
} var layer_style = {
color : 'gray',
cursor : 'text',
textAlign : 'left',
position : 'absolute',
fontSize : $input.css('fontSize'),
fontFamily: $input.css('fontFamily'),
display : is_empty() ? 'block' : 'none'
} // create
var layer_props = {
text : opt.text,
width : width,
height: 'auto'
} // 确保只绑定一次
var ns = '.' + opt.namespace, $layer = $input.data('layer' + ns)
if (!$layer) {
$input.data('layer' + ns, $layer = $('<div>', layer_props).appendTo($input.offsetParent()) )
} // activate
$layer
.css($.extend(layer_style, opt.style))
.unbind('click' + ns)
.bind('click' + ns, function () {
opt.hideOnFocus && hide()
$input.focus()
}) $input
.unbind(ns)
.bind('blur' + ns, check) if (opt.hideOnFocus) {
$input.bind('focus' + ns, hide)
}else{
$input.bind('keypress keydown' + ns, function(e) {
var key = e.keyCode
if (e.charCode || (key >= 65 && key <=90)) {
hide()
}
})
.bind('keyup' + ns,check)
} // 由于 ie 记住密码的特性,需要监听值改变
// ie9 不支持 jq bind 此事件
$input.get(0).onpropertychange = check position()
check()
})
} })(jQuery)

html部分:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<![endif]-->
<title>HTML5 placeholder jQuery Plugin</title>
<style type="text/css">
body, input, textarea {
font: 1em/1.4 Helvetica, Arial;
}
label code {
cursor: pointer;
float: left;
width: 150px;
}
input {
width: 14em;
}
textarea {
height: 5em;
width: 20em;
}
.placeholder {
color: #aaa;
}
.note {
border: 1px solid orange;
padding: 1em;
background: #ffffe0;
}
</style>
</head>
<body>
<h1>
HTML5 Placeholder jQuery Plugin</h1>
<form id="test">
<p>
<label>
<code>type=search</code>
<input type="search" name="search" placeholder="搜索网站…" autofocus></label></p>
<p>
<label>
<code>type=text</code>
<input type="text" name="name" placeholder="输入文本"></label></p>
<p>
<label>
<code>type=email</code>
<input type="email" name="email" placeholder="输入邮箱"></label></p>
<p>
<label>
<code>type=url</code>
<input type="url" name="url" placeholder="输入网址"></label></p>
<p>
<label>
<code>type=tel</code>
<input type="tel" name="tel" placeholder="输入电话号码"></label></p>
<p>
<label for="p">
<code>type=password</code>
</label>
<input type="password" name="password" placeholder="输入密码" id="p"></p>
<p>
<label>
<code>textarea</code>
<textarea name="message" placeholder="文本内容写这里"></textarea></label></p>
<p>
<input type="submit" value="提交"></p>
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="jquery.placeholder.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
var support = (function (input) {
return function (attr) { return attr in input }
})(document.createElement('input'))
if (!(support('placeholder') && $.browser.webkit)) {
$('input[placeholder],textarea[placeholder]').placeholder({
useNative: false,
hideOnFocus: false,
style: {
textShadow: 'none'
}
});
} if (!support('autofocus')) {
$('input[autofocus]').focus()
}
});
</script>
</body>
</html>

(转)html5 Placeholder属性兼容IE6、7方法的更多相关文章

  1. html5 placeholder属性兼容ie11

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

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

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

  3. 关于HTML5标签不兼容(IE6~8)

    HTML5的语义化标签以及属性,可以让开发者非常方便地实现清晰的web页面布局,加上CSS3的效果渲染,快速建立丰富灵活的web页面显得非常简单. 比较常用的HTML5的新标签元素有: <hea ...

  4. jquery 小插件,完成“输入字段预期值的提示信息”,防html5 placeholder属性

    前言:在很多时候,我们需要文本框中显示默认值,获取焦点时,文字框中就会清空给的值,当失去焦点时,如果没有值,继续显示默认的文字,如果有输入值,就显示输入的值.现在项目中需要用到这个地方的功能比较多,于 ...

  5. placeholder属性兼容ie8

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

  6. PNG兼容IE6解决方法

    虽然说现在早就不用ie6浏览器了,可以还是有一小部分还在使用 ,刚好公司也有要求~~~ <p> E6不兼容png图片,确实让网页的图片质量大大下降,为了兼容万恶的IE6,总结了下面几种方法 ...

  7. placeholder属性兼容js支持

    $(function(){ //判断浏览器是否支持placeholder属性 supportPlaceholder='placeholder'in document.createElement('in ...

  8. png兼容IE6的方法

    1.通过CSS滤镜使背景图的PNG对IE6进行兼容 定义一个样式,给某个div应用这个样式后,div的透明png背景图片自动透明了. <style> body{background: li ...

  9. 【转】HTML5新增元素兼容旧浏览器方法

    ref:http://www.jb51.net/html5/163906.html 问题:如何让IE8-兼容这些标签?(需要设计JS中的DOM) 代码如下: <span style=" ...

随机推荐

  1. Hibernate的CRUD

    1.CRUD: C:sesion.save() R:session.get()? session.load() D:session.delete() U:session.update() 2.读取数据 ...

  2. 编写可维护的javascript代码--- 2015.11.22(注释)

    1.单行注释 // 这是一句单行注释 2.多行注释 /* 这里是代码 */     /*  这里都是注释 1232132  */      java的注释风格 /* * 另一段注释 * 这段注释包含2 ...

  3. win7 IIS7.0 【IIS 管理器无法验证此内置帐户是否有访问权】

    异常信息: 服务器配置为将传递身份验证和内置帐户一起使用,以访问指定的物理路径.但是,IIS 管理器无法验证此内置帐户是否有访问权.请确保应用程序池标识具有该物理路径的读取访问权.如果此服务器加入到域 ...

  4. UITableView的详细使用

    UITableView的详细使用   UITableView是app开发中常用到的控件,功能很强大,多用于数据的显示.下面以一个简单的实例来介绍tableview的基本用法.(适合新手,高手飘过) @ ...

  5. spring mvc标准项目结构

    src com.xxx.inews.dao com.xxx.inews.dao.impl com.xxx.inews.data.entity com.xxx.inews.data.vo com.xxx ...

  6. UE标签排列方式

        UE是一款很顺手的文件编辑软件.打开文本,都会有个标签.根据个人喜好,我喜欢标签都展开,方便快速选取指定文本. 设置如下: 高级>配置>应用程序布局>可停靠窗口>类型 ...

  7. 转:有事务处理的NoSQL数据库

    原文来自于:http://www.infoq.com/cn/articles/MarkLogic-NoSQL-with-Transactions Java平台在其几乎整个生命周期中,都在煞费苦心地努力 ...

  8. KeilMDK4.7a下载地址/中文乱码解决/自动关联设置

    推荐地址1,速度更快(确定为4.7a版本,且含注册机):1.http://www.mcuzone.com/software/keil/MDK470.RAR 2.http://kuai.xunlei.c ...

  9. opencv 用户文档 错误更正 仿射变换

    今天在看opencv官方给出的仿射变换计算仿射变换矩阵的文档的时候,发现官方文档中有个很明显的错误,再次给大家提个醒. 官方文档连接: http://opencv.willowgarage.com/d ...

  10. Centos6.5 qt 安装

    1,centos linux系统必须预先以安装x Server(KDE or GNOME) 2,wget http://download.qt-project.org/official_release ...