DIV CSS 佈局教程網

點擊輸入框就讓 placeholder 提示消失:兩種相容性解法與原生屬性對比
編輯:關於JavaScript     

在 HTML 中,placeholder 作為 input 的一個屬性,起到在輸入框內佔位並提示的作用。

但有些瀏覽器(例如 Chrome)在滑鼠點擊輸入框時,placeholder 的值不會消失,只有開始輸入資料才會消失,這會大大折損前端的使用者體驗。

看過不少高手的做法,動輒寫一大段 JS,讀起來有點吃力,於是筆者想到了一個最「笨」卻行之有效的解法,順利解決了這個問題。

HTML 程式碼:

 <input type="text" placeholder="多個關鍵詞空格隔開"> 

滑鼠點擊 input 時,讓 placeholder 中的提示資訊消失:

 <input type="text" placeholder="多個關鍵詞空格隔開" onfocus="this.placeholder=‘‘" onblur="this.placeholder=‘多個關鍵詞空格隔開‘">

PlaceHolder 的兩種實現方式

placeholder 屬性是 HTML5 為 input 新增的,它在輸入框上提供一個佔位符,以文字形式展示該欄位預期值的提示(hint),當欄位為空時便會顯示。

例如:

 <input type="text" name="loginName" placeholder="郵箱/手機號/QQ號"> 

目前各瀏覽器的支援情況

然而,雖然 IE10+ 支援 placeholder 屬性,它的表現卻與其他瀏覽器並不一致:

•IE10+ 在滑鼠點擊(取得焦點)時,placeholder 文字會消失;
•Firefox/Chrome/Safari 點擊不消失,而是鍵盤輸入時文字才消失。

這著實惱人。如果使用了 placeholder 屬性,產品經理還是不依不饒,會追問為什麼 IE 裡是點擊時提示文字消失,Chrome 裡卻是鍵盤輸入時才消失,要求前端工程師改成一致的表現。基於此,以下兩種實作方式都放棄了原生的 placeholder 屬性。

兩種方式的思路

1.(方式一)使用 input 的 value 作為顯示文字

2.(方式二)不使用 value,而是在 body 裡額外加入一個標籤(span),再以絕對定位覆蓋到 input 上面

兩種方式各有優缺點:方式一佔用了 input 的 value 屬性,表單提交時需要額外做一些判斷工作;方式二則使用了額外的標籤。

方式一

 /** * PlaceHolder組件 * $(input).placeholder({ * word: // @string 提示文本 * color: // @string 文本顏色 * evtType: // @string focus|keydown 觸發placeholder的事件類型 * }) * * NOTE: * evtType默認是focus,即鼠標點擊到輸入域時默認文本消失,keydown則模擬HTML5 placeholder屬性在Firefox/Chrome裡的特征,光標定位到輸入域後鍵盤輸入時默認文本才消失。 * 此外,對於HTML5 placeholder屬性,IE10+和Firefox/Chrome/Safari的表現形式也不一致,因此內部實現不采用原生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() { // 光標定位到首位 $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() // 位置調整 move() // textarea 不加line-heihgt屬性 if ($that.is('input')) { $placeholder.css({ lineHeight: height + 'px' }) } $placeholder.appendTo(document.body) // 內容為空時才顯示,比如刷新頁面輸入域已經填入了內容時 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() // 蓋住後無法觸發input的click事件,需要模擬點擊下 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() } }) // 窗口縮放時處理 $(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) }) }

方式二對於以下場景並不適用

1. input 初始隱藏

此時無法取得 input 的 offset,進而無法定位 span 到 input 上方。

2. 包含 input 的頁面 DOM 結構發生變化

比如頁面裡刪除或新增了一些元素,導致 input 向上或向下偏移,而此時 span 卻沒有偏移(因為 span 是相對 body 定位的)。這種情況比較棘手,可以考慮把 span 作為 input 的兄弟元素,也就是相對內層 div 定位(而非 body)。但這樣就必須強制給外層 div 加上 position:relative,而這又可能對頁面佈局產生一定影響。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved