placeholder属性是HTML5 中为input添加的。在input上提供一个占位符,文字形式展示输入字段预期值的提示信息(hint),改字段会在输入为空时显示。
如
<input type="text" name="loginName" placeholder="邮箱/手机号/QQ号">
目前浏览器的支持情况
| 浏览器 | IE6/7/8/9 | IE10+ | Firefox | Chrome | Safari |
| 是否支持 | NO | Yes | YES | YES | YES |
然而,虽然IE10+支持placeholder属性,它的表现与其它浏览器也不一致
这相当恶心,如果使用了placeholder属性。产品经理还是不依不饶,会将为什么IE里是点击文本消失,Chrome里是输入文本消失。应该做成一致的表现形式。鉴于此,以下两种实现方式均不采用原生的placeholder属性。
两种方式的思路
两种方式各有优缺点,方式一占用了input的value属性,表单提交时需要额外做一些判断工作,方式二则使用了额外的标签。
$.fn.placeholder = function(option, callback) {
var settings = $.extend({
word: ‘‘,
color: ‘#ccc‘,
evtType: ‘focus‘
}, option)
function bootstrap($that) {
// some alias
var word = settings.word
var color = settings.color
var evtType = settings.evtType
// default
var defColor = $that.css(‘color‘)
var defVal = $that.val()
if (defVal == ‘‘ || defVal == word) {
$that.css({color: color}).val(word)
} else {
$that.css({color: defColor})
}
function switchStatus(isDef) {
if (isDef) {
$that.val(‘‘).css({color: defColor})
} else {
$that.val(word).css({color: color})
}
}
function asFocus() {
$that.bind(evtType, function() {
var txt = $that.val()
if (txt == word) {
switchStatus(true)
} else {
switchStatus(false)
}
}).bind(‘blur‘, function() {
var txt = $that.val()
if (txt == ‘‘) {
switchStatus(false)
}
})
}
function asKeydown() {
$that.bind(‘focus‘, function() {
var elem = $that[0]
var val = $that.val()
if (val == word) {
setTimeout(function() {
// 光标定位到首位
$that.setCursorPosition({index: 0})
// keydown事件里处理placeholder
$that.keydown(function() {
var val = $that.val()
if (val == word) {
switchStatus(true)
}
}).keyup(function() {
var val = $that.val()
if (val == ‘‘) {
switchStatus(false)
$that.setCursorPosition({index: 0})
}
})
}, 10)
}
})
}
if (evtType == ‘focus‘) {
asFocus()
} else if (evtType == ‘keydown‘) {
asKeydown()
}
}
return this.each(function() {
var $elem = $(this)
bootstrap($elem)
if ($.isFunction(callback)) callback($elem)
})
}
线上示例:http://snandy.github.io/lib/ui/demo/placeholder/b.html
会用到 setCursorPosition。
$.fn.placeholder = function(option, callback) {
var settings = $.extend({
word: ‘‘,
color: ‘#999‘,
evtType: ‘focus‘,
zIndex: 20,
diffPaddingLeft: 3
}, option)
function bootstrap($that) {
// some alias
var word = settings.word
var color = settings.color
var evtType = settings.evtType
var zIndex = settings.zIndex
var diffPaddingLeft = settings.diffPaddingLeft
// default css
var offset = $that.offset()
var top = offset.top
var left = offset.left
var width = $that.outerWidth()
var height = $that.outerHeight()
var fontSize = $that.css(‘font-size‘)
var fontFamily = $that.css(‘font-family‘)
var paddingLeft = $that.css(‘padding-left‘)
// process
paddingLeft = parseInt(paddingLeft, 10) + diffPaddingLeft
// redner
var $placeholder = $(‘<span>‘)
$placeholder.css({
position: ‘absolute‘,
zIndex: ‘20‘,
top: top,
left: left,
color: color,
width: (width - paddingLeft) + ‘px‘,
height: height + ‘px‘,
fontSize: fontSize,
paddingLeft: paddingLeft + ‘px‘,
fontFamily: fontFamily
}).text(word).hide()
// textarea 不加line-heihgt属性
if ($that.is(‘input‘)) {
$placeholder.css({
lineHeight: height + ‘px‘
})
}
$placeholder.appendTo(document.body)
// 内容为空时才显示,比如刷新页面输入域已经填入了内容时
var val = $that.val()
if (val == ‘‘) {
$placeholder.show()
}
function hideAndFocus() {
$placeholder.hide()
$that[0].focus()
}
function asFocus() {
$placeholder.click(hideAndFocus)
// IE有些bug,原本不用加此句
$that.click(hideAndFocus)
$that.blur(function() {
var txt = $that.val()
if (txt == ‘‘) {
$placeholder.show()
}
})
}
function asKeydown() {
$placeholder.click(function() {
$that[0].focus()
})
}
if (evtType == ‘focus‘) {
asFocus()
} else if (evtType == ‘keydown‘) {
asKeydown()
}
$that.keyup(function() {
var txt = $that.val()
if (txt == ‘‘) {
$placeholder.show()
} else {
$placeholder.hide()
}
})
}
return this.each(function() {
var $elem = $(this)
bootstrap($elem)
if ($.isFunction(callback)) callback($elem)
})
}
线上示例:http://snandy.github.io/lib/ui/demo/placeholder/a.html
相关:
http://www.w3.org/TR/2009/WD-html5-20090825/forms.html#the-placeholder-attribute
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-placeholder
原文:http://www.cnblogs.com/snandy/p/4115883.html