jQuery封裝placeholder效果實(shí)現(xiàn)方法,讓低版本瀏覽器支持該效果
頁面中的輸入框默認(rèn)的提示文字一般使用placeholder屬性就可以了,即:
<input type="text" name="username" placeholder="請(qǐng)輸入用戶名" value="" id="username"/>
最多加點(diǎn)樣式控制下默認(rèn)文字的顏色
input::-webkit-input-placeholder{color:#AAAAAA;}
但是在低版本的瀏覽器卻不支持這個(gè)placeholder屬性,那么真的要在低版本瀏覽器也要實(shí)現(xiàn)跟placeholder一樣的效果,就需要寫個(gè)插件來兼容下,下面就細(xì)講一下怎樣用jquery來實(shí)現(xiàn)這個(gè)模擬效果。
實(shí)現(xiàn)這個(gè)模擬效果,頁面的一般調(diào)用方式:
$('input').placeholder();
首先,先寫jquery插件的一般結(jié)構(gòu):
;(function($){ $.fn.placeholder = function(){ //實(shí)現(xiàn)placeholder的代碼 } })(jQuery)
下面我們就要判斷瀏覽器是否支持placeholder屬性。
;(function($){ $.fn.placeholder = function(){ this.each(function(){ var _this = this; var supportPlaceholder = 'placeholder' in document.createElement('input'); if(!supportPlaceholder){ //不支持placeholder屬性的操作 } }); } })(jQuery)
我們要支持鏈?zhǔn)讲僮鳎缦拢?/p>
;(function($){ $.fn.placeholder = function(){ return this.each(function(){ var _this = this; var supportPlaceholder = 'placeholder' in document.createElement('input'); if(!supportPlaceholder){ //不支持placeholder屬性的操作 } }); } })(jQuery)
默認(rèn)配置項(xiàng):
options = $.extend({ placeholderColor:'#aaaaaa', isSpan:false, //是否使用插入span標(biāo)簽?zāi)Mplaceholder的方式,默認(rèn)是不需要 onInput:true //實(shí)時(shí)監(jiān)聽輸入框 },options);
如果不需要通過span來模擬placeholder效果,那么就需要通過輸入框的value值來判斷,如下代碼:
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標(biāo)簽來模擬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把當(dāng)前$simulationSpan添加到$(_this)前面,并讓$(_this)聚焦 $(_this).before($simulationSpan.click(function () { $(_this).trigger('focus'); })); //當(dāng)前輸入框聚焦文本內(nèi)容不為空時(shí),模擬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標(biāo)簽?zāi)Mplaceholder的方式,默認(rèn)是不需要 onInput:true //實(shí)時(shí)監(jiān)聽輸入框 },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把當(dāng)前$simulationSpan添加到$(_this)前面,并讓$(_this)聚焦 $(_this).before($simulationSpan.click(function () { $(_this).trigger('focus'); })); //當(dāng)前輸入框聚焦文本內(nèi)容不為空時(shí),模擬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);
調(diào)用方式,需要通過span標(biāo)簽來模擬的話:
$("#username").placeholder({ isSpan:true });
以上這篇jQuery封裝placeholder效果實(shí)現(xiàn)方法,讓低版本瀏覽器支持該效果就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決jquery操作checkbox火狐下第二次無法勾選問題
在工作中使用jquery操作checkbox,進(jìn)行全選、反選,現(xiàn)在的問題是火狐下第二次無法勾選問題,在下面有個(gè)詳細(xì)的解答,感興趣的朋友可以參考下2014-02-02用jquery ajax獲取網(wǎng)站Alexa排名的代碼
其實(shí)就是利用了jquery的ajax功能,獲取遠(yuǎn)程的xml文件,讀取指定內(nèi)容的代碼,對(duì)于學(xué)習(xí)jquery的朋友是個(gè)不錯(cuò)的參考。2009-12-12jQuery對(duì)象的length屬性用法實(shí)例
這篇文章主要介紹了jQuery對(duì)象的length屬性用法,實(shí)例分析了length屬性獲取集合中對(duì)象數(shù)目的使用技巧,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12MultiSelect左右選擇控件的設(shè)計(jì)與實(shí)現(xiàn)介紹
由于項(xiàng)目中用到左右選擇的控件,網(wǎng)上找了一些相關(guān)的實(shí)現(xiàn),基本上有兩個(gè):1、基于JQuery UI的控件2、某個(gè)兄弟手寫的一個(gè)控件,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06實(shí)例講解JQuery中this和$(this)區(qū)別
這篇文章主要介紹了實(shí)例講解JQuery中this和$(this)的區(qū)別,this表示當(dāng)前的上下文對(duì)象是一個(gè)html對(duì)象,可以調(diào)用html對(duì)象所擁有的屬性和方法,$(this),代表的上下文對(duì)象是一個(gè)jquery的上下文對(duì)象,可以調(diào)用jquery的方法和屬性值,需要的朋友可以參考下2014-12-12jQuery zTree搜索-關(guān)鍵字查詢 遞歸無限層功能實(shí)現(xiàn)代碼
這篇文章主要介紹了zTree搜索功能 -- 關(guān)鍵字查詢 -- 遞歸無限層的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01jQuery點(diǎn)擊后一組圖片左右滑動(dòng)的實(shí)現(xiàn)代碼
主要是用作圖片展示的時(shí)候需要,點(diǎn)擊后一組圖片左右滑動(dòng),需要的朋友可以參考下2012-08-08jQuery使用$.get()方法從服務(wù)器文件載入數(shù)據(jù)實(shí)例
這篇文章主要介紹了jQuery使用$.get()方法從服務(wù)器文件載入數(shù)據(jù),較為簡(jiǎn)單的分析了jQuery中g(shù)et方法的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03基于Jquery的$.cookie()實(shí)現(xiàn)跨越頁面tabs導(dǎo)航實(shí)現(xiàn)代碼
基于Jquery的$.cookie()實(shí)現(xiàn)跨越頁面tabs導(dǎo)航實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-03-03