input點(diǎn)擊后placeholder中的提示消息消失
html中,placeholder作為input的一個(gè)屬性,起到了在輸入框中占位并提示的作用。
但是有一些瀏覽器,如chrome,當(dāng)鼠標(biāo)點(diǎn)擊輸入框時(shí),placeholder的值不消失,只有輸入數(shù)據(jù)才消失,會(huì)使前端用戶體驗(yàn)大打折扣。
看了很多大神的方法,寫了長(zhǎng)長(zhǎng)的js,看著有點(diǎn)吃力,就想到了下面這種最傻的方法解決了這個(gè)問(wèn)題。
html代碼:
<input type="text" placeholder="多個(gè)關(guān)鍵詞空格隔開">
鼠標(biāo)點(diǎn)擊input時(shí),placeholder中的提示信息消失:
<input type="text" placeholder="多個(gè)關(guān)鍵詞空格隔開" onfocus="this.placeholder=‘‘" onblur="this.placeholder=‘多個(gè)關(guān)鍵詞空格隔開‘">
PlaceHolder的兩種實(shí)現(xiàn)方式
placeholder屬性是HTML5 中為input添加的。在input上提供一個(gè)占位符,文字形式展示輸入字段預(yù)期值的提示信息(hint),該字段會(huì)在輸入為空時(shí)顯示。
如
<input type="text" name="loginName" placeholder="郵箱/手機(jī)號(hào)/QQ號(hào)">
目前瀏覽器的支持情況
然而,雖然IE10+支持placeholder屬性,它的表現(xiàn)與其它瀏覽器也不一致
•IE10+里鼠標(biāo)點(diǎn)擊時(shí)(獲取焦點(diǎn))placeholder文本消失
•Firefox/Chrome/Safari點(diǎn)擊不消失,而是鍵盤輸入時(shí)文本消失
這相當(dāng)惡心,如果使用了placeholder屬性。產(chǎn)品經(jīng)理還是不依不饒,會(huì)講為什么IE里是點(diǎn)擊的時(shí)候提示文本消失,Chrome里卻是鍵盤輸入的時(shí)候提示文本消失。要求前端工程師改成一樣的表現(xiàn)形式。鑒于此,以下兩種實(shí)現(xiàn)方式均不采用原生的placeholder屬性。
兩種方式的思路
1.(方式一)使用input的value作為顯示文本
2.(方式二)不使用value,添加一個(gè)額外的標(biāo)簽(span)到body里然后絕對(duì)定位覆蓋到input上面
兩種方式各有優(yōu)缺點(diǎn),方式一占用了input的value屬性,表單提交時(shí)需要額外做一些判斷工作,方式二則使用了額外的標(biāo)簽。
方式一
/**
* PlaceHolder組件
* $(input).placeholder({
* word: // @string 提示文本
* color: // @string 文本顏色
* evtType: // @string focus|keydown 觸發(fā)placeholder的事件類型
* })
*
* NOTE:
* evtType默認(rèn)是focus,即鼠標(biāo)點(diǎn)擊到輸入域時(shí)默認(rèn)文本消失,keydown則模擬HTML5 placeholder屬性在Firefox/Chrome里的特征,光標(biāo)定位到輸入域后鍵盤輸入時(shí)默認(rèn)文本才消失。
* 此外,對(duì)于HTML5 placeholder屬性,IE10+和Firefox/Chrome/Safari的表現(xiàn)形式也不一致,因此內(nèi)部實(shí)現(xiàn)不采用原生placeholder屬性
*/
$.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)
}
}).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() {
// 光標(biāo)定位到首位
$that.setCursorPosition({index: 0})
}, 10)
}
})
}
if (evtType == 'focus') {
asFocus()
} else if (evtType == 'keydown') {
asKeydown()
}
// 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})
}
})
}
return this.each(function() {
var $elem = $(this)
bootstrap($elem)
if ($.isFunction(callback)) callback($elem)
})
}
方式二
$.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 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 class="placeholder">')
$placeholder.css({
position: 'absolute',
zIndex: '20',
color: color,
width: (width - paddingLeft) + 'px',
height: height + 'px',
fontSize: fontSize,
paddingLeft: paddingLeft + 'px',
fontFamily: fontFamily
}).text(word).hide()
// 位置調(diào)整
move()
// textarea 不加line-heihgt屬性
if ($that.is('input')) {
$placeholder.css({
lineHeight: height + 'px'
})
}
$placeholder.appendTo(document.body)
// 內(nèi)容為空時(shí)才顯示,比如刷新頁(yè)面輸入域已經(jīng)填入了內(nèi)容時(shí)
var val = $that.val()
if ( val == '' && $that.is(':visible') ) {
$placeholder.show()
}
function hideAndFocus() {
$placeholder.hide()
$that[0].focus()
}
function move() {
var offset = $that.offset()
var top = offset.top
var left = offset.left
$placeholder.css({
top: top,
left: left
})
}
function asFocus() {
$placeholder.click(function() {
hideAndFocus()
// 蓋住后無(wú)法觸發(fā)input的click事件,需要模擬點(diǎn)擊下
setTimeout(function(){
$that.click()
}, 100)
})
// 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()
}
})
// 窗口縮放時(shí)處理
$(window).resize(function() {
move()
})
// cache
$that.data('el', $placeholder)
$that.data('move', move)
}
return this.each(function() {
var $elem = $(this)
bootstrap($elem)
if ($.isFunction(callback)) callback($elem)
})
}
方式2 對(duì)于以下場(chǎng)景不適合
1. input初始隱藏
此時(shí)無(wú)法取到input的offset,繼而無(wú)法定位span到input上面。
2. 包含input的頁(yè)面dom結(jié)構(gòu)發(fā)生變化
比如頁(yè)面里刪除了一些元素或添加了一些元素,導(dǎo)致input向上或向下偏移,而此時(shí)span則沒(méi)有偏移(span相對(duì)body定位)。這比較惡心,可以考慮把span作為input的兄弟元素,即相對(duì)內(nèi)層div定位(而不是body)。但這樣必須強(qiáng)制給外層div添加position:relative,添加后可能會(huì)對(duì)頁(yè)面布局產(chǎn)生一定影響。
相關(guān)文章
JavaScript實(shí)現(xiàn)系統(tǒng)防掛機(jī)(無(wú)操作彈窗)的示例詳解
在一些學(xué)習(xí)系統(tǒng),或者考試系統(tǒng)中。一旦出現(xiàn)長(zhǎng)時(shí)間未操作,就會(huì)判定這個(gè)人不在場(chǎng)。所以就會(huì)進(jìn)行退出系統(tǒng),處于對(duì)安全和系統(tǒng)負(fù)擔(dān)還有業(yè)務(wù)的需求。本文就來(lái)用JavaScript做一個(gè)系統(tǒng)防掛機(jī)功能,需要的可以參考一下2023-01-01
JavaScript 申明函數(shù)的三種方法 每個(gè)函數(shù)就是一個(gè)對(duì)象(一)
JavaScript 申明函數(shù)的三種方法 每個(gè)函數(shù)就是一個(gè)對(duì)象(一)2009-12-12
javascript實(shí)現(xiàn)俄羅斯方塊游戲的思路和方法
至于俄羅斯方塊的話,有很多的難點(diǎn),如果有JS去寫的話,要考慮到碰撞啊,邊界啊,下落等問(wèn)題,本文這些問(wèn)題大部分都有考慮到,這里給大家提供一部分思路,拋磚引玉,有需要的小伙伴可以參考下。2015-04-04
基于JS開發(fā)微信網(wǎng)頁(yè)錄音功能的實(shí)例代碼
這篇文章主要介紹了基于JS開發(fā)微信網(wǎng)頁(yè)錄音功能的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
uniapp實(shí)現(xiàn)上拉加載更多功能的全過(guò)程
我們?cè)陧?xiàng)目中經(jīng)常使用到上拉加載更多,下面這篇文章主要給大家介紹了關(guān)于uniapp實(shí)現(xiàn)上拉加載更多功能的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10
預(yù)防網(wǎng)頁(yè)掛馬的方法總結(jié)
這篇文章主要介紹了預(yù)防網(wǎng)頁(yè)掛馬的方法總結(jié)的相關(guān)資料,這里詳細(xì)整理了具體方法,和如何實(shí)現(xiàn),需要的朋友可以參考下2016-11-11
JS面向?qū)ο缶幊袒A(chǔ)篇(三) 繼承操作實(shí)例詳解
這篇文章主要介紹了JS面向?qū)ο缶幊汤^承操作,結(jié)合實(shí)例形式詳細(xì)分析了JS面向?qū)ο罄^承的具體概念、原理、實(shí)現(xiàn)方法及操作注意事項(xiàng),需要的朋友可以參考下2020-03-03
qrcode生成二維碼微信長(zhǎng)按無(wú)法識(shí)別問(wèn)題的解決
這篇文章主要介紹了qrcode生成二維碼微信長(zhǎng)按無(wú)法識(shí)別問(wèn)題的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

