頁面中的輸入框默認的提示文字通常使用placeholder
屬性就能夠了,即:css
<input type="text" name="username" placeholder="請輸入用戶名" value="" id="username"/>
最多加點樣式控制下默認文字的顏色node
input::-webkit-input-placeholder{color:#AAAAAA;}
可是在低版本的瀏覽器卻不支持這個placeholder
屬性,那麼真的要在低版本瀏覽器也要實現跟placeholder
同樣的效果,就須要寫個插件來兼容下,下面就細講一下怎樣用jquery來實現這個模擬效果。jquery
實現這個模擬效果,頁面的通常調用方式:web
$('input').placeholder();
首先,先寫jquery插件的通常結構:瀏覽器
;(function($){ $.fn.placeholder = function(){ //實現placeholder的代碼 } })(jQuery)
下面咱們就要判斷瀏覽器是否支持placeholder屬性
。jquery插件
;(function($){ $.fn.placeholder = function(){ this.each(function(){ var _this = this; var supportPlaceholder = 'placeholder' in document.createElement('input'); if(!supportPlaceholder){ //不支持placeholder屬性的操做 } }); } })(jQuery)
咱們要支持鏈式操做,以下:ide
;(function($){ $.fn.placeholder = function(){ return this.each(function(){ var _this = this; var supportPlaceholder = 'placeholder' in document.createElement('input'); if(!supportPlaceholder){ //不支持placeholder屬性的操做 } }); } })(jQuery)
默認配置項:this
options = $.extend({ placeholderColor:'#aaaaaa', isSpan:false, //是否使用插入span標籤模擬placeholder的方式,默認是不須要 onInput:true //實時監聽輸入框 },options);
若是不須要經過span來模擬placeholder
效果,那麼就須要經過輸入框的value值來判斷,以下代碼:spa
if(!options.isSpan){ $(_this).focus(function () { var pattern = new RegExp("^" + defaultValue + "$|^$"); pattern.test($(_this).val()) && $(_this).val('').css('color', defaultColor); }).blur(function () { if($(_this).val() == defaultValue) { $(_this).css('color', defaultColor); } else if($(_this).val().length == 0) { $(_this).val(defaultValue).css('color', options.placeholderColor) } }).trigger('blur'); }
若是須要同span標籤來模擬placeholder
效果,代碼以下:插件
var $simulationSpan = $('<span class="wrap-placeholder">'+defaultValue+'</span>'); $simulationSpan.css({ 'position':'absolute', 'display':'inline-block', 'overflow':'hidden', 'width':$(_this).outerWidth(), 'height':$(_this).outerHeight(), 'color':options.placeholderColor, 'margin-left':$(_this).css('margin-left'), 'margin-top':$(_this).css('margin-top'), 'padding-left':parseInt($(_this).css('padding-left')) + 2 + 'px', 'padding-top':_this.nodeName.toLowerCase() == 'textarea' ? parseInt($(_this).css('padding-top')) + 2 : 0, 'line-height':_this.nodeName.toLowerCase() == 'textarea' ? $(_this).css('line-weight') : $(_this).outerHeight() + 'px', 'font-size':$(_this).css('font-size'), 'font-family':$(_this).css('font-family'), 'font-weight':$(_this).css('font-weight') }); //經過before把當前$simulationSpan添加到$(_this)前面,並讓$(_this)聚焦 $(_this).before($simulationSpan.click(function () { $(_this).trigger('focus'); })); //當前輸入框聚焦文本內容不爲空時,模擬span隱藏 $(_this).val().length != 0 && $simulationSpan.hide(); if (options.onInput) { //綁定oninput/onpropertychange事件 var inputChangeEvent = typeof(_this.oninput) == 'object' ? 'input' : 'propertychange'; $(_this).bind(inputChangeEvent, function () { $simulationSpan[0].style.display = $(_this).val().length != 0 ? 'none' : 'inline-block'; }); }else { $(_this).focus(function () { $simulationSpan.hide(); }).blur(function () { /^$/.test($(_this).val()) && $simulationSpan.show(); }); };
總體代碼:
;(function($){ $.fn.placeholder = function(options){ options = $.extend({ placeholderColor:'#aaaaaa', isSpan:false, //是否使用插入span標籤模擬placeholder的方式,默認是不須要 onInput:true //實時監聽輸入框 },options); return this.each(function(){ var _this = this; var supportPlaceholder = 'placeholder' in document.createElement('input'); if(!supportPlaceholder){ //不支持placeholder屬性的操做 var defaultValue = $(_this).attr('placeholder'); var defaultColor = $(_this).css('color'); if(!options.isSpan){ $(_this).focus(function () { var pattern = new RegExp("^" + defaultValue + "$|^$"); pattern.test($(_this).val()) && $(_this).val('').css('color', defaultColor); }).blur(function () { if($(_this).val() == defaultValue) { $(_this).css('color', defaultColor); } else if($(_this).val().length == 0) { $(_this).val(defaultValue).css('color', options.placeholderColor) } }).trigger('blur'); }else{ var $simulationSpan = $('<span class="wrap-placeholder">'+defaultValue+'</span>'); $simulationSpan.css({ 'position':'absolute', 'display':'inline-block', 'overflow':'hidden', 'width':$(_this).outerWidth(), 'height':$(_this).outerHeight(), 'color':options.placeholderColor, 'margin-left':$(_this).css('margin-left'), 'margin-top':$(_this).css('margin-top'), 'padding-left':parseInt($(_this).css('padding-left')) + 2 + 'px', 'padding-top':_this.nodeName.toLowerCase() == 'textarea' ? parseInt($(_this).css('padding-top')) + 2 : 0, 'line-height':_this.nodeName.toLowerCase() == 'textarea' ? $(_this).css('line-weight') : $(_this).outerHeight() + 'px', 'font-size':$(_this).css('font-size'), 'font-family':$(_this).css('font-family'), 'font-weight':$(_this).css('font-weight') }); //經過before把當前$simulationSpan添加到$(_this)前面,並讓$(_this)聚焦 $(_this).before($simulationSpan.click(function () { $(_this).trigger('focus'); })); //當前輸入框聚焦文本內容不爲空時,模擬span隱藏 $(_this).val().length != 0 && $simulationSpan.hide(); if (options.onInput) { //綁定oninput/onpropertychange事件 var inputChangeEvent = typeof(_this.oninput) == 'object' ? 'input' : 'propertychange'; $(_this).bind(inputChangeEvent, function () { $simulationSpan[0].style.display = $(_this).val().length != 0 ? 'none' : 'inline-block'; }); }else { $(_this).focus(function () { $simulationSpan.hide(); }).blur(function () { /^$/.test($(_this).val()) && $simulationSpan.show(); }); }; } } }); } })(jQuery);
調用方式,須要經過span標籤來模擬:
$("#username").placeholder({ isSpan:true });