$. =
function
(option, callback) {
var
settings = $.extend({
word:
''
,
color:
'#999'
,
evtType:
'focus'
,
zIndex: 20,
diffPaddingLeft: 3
}, option)
function
bootstrap($that) {
// some alias
var
word =
var
color =
var
evtType =
var
zIndex =
var
diffPaddingLeft =
// default css
var
width = $()
var
height = $()
var
fontSize = $(
'font-size'
)
var
fontFamily = $(
'font-family'
)
var
paddingLeft = $(
'padding-left'
)
// process
paddingLeft = parseInt(paddingLeft, 10) + diffPaddingLeft
// redner
var
$placeholder = $(
'<span class="placeholder">'
)
$({
position:
'absolute'
,
zIndex:
'20'
,
color: color,
width: (width - paddingLeft) +
'px'
,
height: height +
'px'
,
fontSize: fontSize,
paddingLeft: paddingLeft +
'px'
,
fontFamily: fontFamily
}).text(word).hide()
// 位置调整
move()
// textarea 不加line-heihgt属性
if
($(
'input'
)) {
$({
lineHeight: height +
'px'
})
}
$()
// 内容为空时才显示,比如刷新页面输入域已经填入了内容时
var
val = $()
if
( val ==
''
&& $(
':visible'
) ) {
$()
}
function
hideAndFocus() {
$()
$that[0].focus()
}
function
move() {
var
offset = $()
var
top =
var
left =
$({
top: top,
left: left
})
}
function
asFocus() {
$(
function
() {
hideAndFocus()
// 盖住后无法触发input的click事件,需要模拟点击下
setTimeout(
function
(){
$()
}, 100)
})
// IE有些bug,原本不用加此句
$(hideAndFocus)
$(
function
() {
var
txt = $()
if
(txt ==
''
) {
$()
}
})
}
function
asKeydown() {
$(
function
() {
$that[0].focus()
})
}
if
(evtType ==
'focus'
) {
asFocus()
}
else
if
(evtType ==
'keydown'
) {
asKeydown()
}
$(
function
() {
var
txt = $()
if
(txt ==
''
) {
$()
}
else
{
$()
}
})
// 窗口缩放时处理
$(window).resize(
function
() {
move()
})
// cache
$(
'el'
, $placeholder)
$(
'move'
, move)
}
return
this
.each(
function
() {
var
$elem = $(
this
)
bootstrap($elem)
if
($.isFunction(callback)) callback($elem)
})
}