解决HTML5 Placeholder的方案

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://xyly624.blog.51cto.com/842520/864959

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

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

jquery.placeholder.zhihu.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)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<![endif]-->
<title>HTML5 placeholder jQuery Plugin</title>
<style>
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="Search this site…" autofocus></label></p>
<p><label><code>type=text</code> <input type="text" name="name" placeholder="e.g. John Doe"></label></p>
<p><label><code>type=email</code> <input type="email" name="email" placeholder="e.g. [email protected]"></label></p>
<p><label><code>type=url</code> <input type="url" name="url" placeholder="e.g. http://mathiasbynens.be/"></label></p>
<p><label><code>type=tel</code> <input type="tel" name="tel" placeholder="e.g. +32 472 77 69 88"></label></p>
<p><label for="p"><code>type=password</code> </label><input type="password" name="password" placeholder="e.g. hunter2" id="p"></p>
<p><label><code>textarea</code> <textarea name="message" placeholder="Your message goes here"></textarea></label></p>
<p><input type="submit" value="type=submit"></p>
</form>
<script src="js/jquery-1.7.2.min.js"></script>
<script src="js/jquery.placeholder.zhihu.js"></script>
<script>
$(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>

时间: 2024-08-14 18:18:17

解决HTML5 Placeholder的方案的相关文章

解决HTML5中placeholder属性兼容性的JQuery插件

//调用方法 $(function () {   $(".pHolder").jason(); }); //HTML代码 <input type="text" class="pHolder" placeholder="请输入姓名" /> //jquery插件 ($.fn.jason = function(a) {    var b = {        focus: "black",      

解决ie不支持HTML5 placeholder的问题

https://github.com/mathiasbynens/jquery-placeholder 解决ie不支持HTML5 placeholder的问题Requires jQuery 1.6+. For an older version of this plugin that works under jQuery 1.4.2+, see v1.8.7.Works in all A-grade browsers, including IE6.

IE8 不支持html5 placeholder的解决方案

IE8不支持html5 placeholder的解决方法. /** * jQuery EnPlaceholder plug * version 1.0 2014.07.01戈志刚 * by Frans.Lee <[email protected]> http://www.ifrans.cn */ (function ($) { $.fn.extend({ "iePlaceholder":function (options) { options = $.extend({ pl

HTML5 placeholder

一.HTML5 placeholder相关的引言 placeholder在英汉词典中解释成了“占位符”.要理解并不难,请看此场景: “咦?”您可能会疑问,“这不是就是狗狗树下撒尿尿”.确实,该场景可以较好的诠释placeholder“占位符”之意.我们不妨将placeholder来个临时拆分:place + hold + er. 对于上面场景就有:place指树及附近区域:hold表示镇住了其他狗狗——此处为洒家的地盘:而er则将重点转到了尿上.于是,在这个例子中,placeholder指的就是

HTML5 placeholder实际应用经验分享及拓展

一.HTML5 placeholder介绍 placeholder在英汉词典中解释成了"占位符".我们不妨将placeholder来个临时拆分:place + hold + er.placeholder指的就是:"足以镇住这块区占据位置的字符.无论是传统软件或是web应用中,placeholder都是相当常见的. 如FireFox浏览器右上方的搜索占位符: 在可输入控件中,"占位符"一般作提示用,简洁明了,空间利用率高.然而,在XHTML的时代,控件元素并

html5 placeholder ie10以下不兼容

/*html5 placeholder ie10以下不兼容 加上这个jquery代码可以解决兼容*/ $(function(){ if(!placeholderSupport()){ // 判断浏览器是否支持 placeholder $('[placeholder]').focus(function() { var input = $(this); if (input.val() == input.attr('placeholder')) { input.val(''); input.remov

debian7 oracle11g 解决 link binaries 错误方案

ln -s /etc /etc/rc.d ln -s /usr/bin/awk /bin/awk ln -s /usr/bin/basename /bin/basename ln -s /usr/bin/rpm /bin/rpm ln -s /lib/i386-linux-gnu/libgcc_s.so.1 /usr/lib ln -s /usr/lib/i386-linux-gnu/libc_nonshared.a /usr/lib ln -s /usr/lib/i386-linux-gnu/

HTML5 Placeholder实现input背景文字提示效果

这篇文章我们来看看什么是input输入框背景文字提示效果,如下图所示: 这种效果现在网上非常的普遍流行,但大部分是使用JavaScript实现的.但HTML5给我们提供了新的纯HTML的实现方式,不需要任何的JavaScript,只需要在你的input文本框的标记上添加HTML5规范里新增的placeholder属性,然后在属性值里输入你需要的提示信息. 语法基本是这个样子:<input placeholder=”提示信息...”> HTML代码 <form> <input

解决html5新标签 placeholder 低版本浏览器下不兼容问题

placeholder属性是HTML5 中为input添加的.在input上提供一个占位符,文字形式展示输入字段预期值的提示信息(hint),该字段会在输入为空时显示. 实例:1 <input type="text" name="userName" placeholder="请输入用户名"> placeholder操作起来非常方便,提高了开发效率,同时在高版本浏览器中用户体验也很好,所以本人很喜欢用这个属性. 然而,在IE9以下版本浏