jQuery封裝placeholder的實例程式碼
頁面中的輸入框預設的提示文字一般使用placeholder
屬性就可以了,即:
<input type="text" name="username" placeholder="请输入用户名" value="" id="username"/>
最多加點樣式控制下預設文字的顏色
input::-webkit-input-placeholder{color:#AAAAAA;}
但是低版的瀏覽器卻不支援這個placeholder
屬性,那麼真的要在低版瀏覽器也要實作跟placeholder
一樣的效果,就需要寫個插件來相容下,下面就細講一下怎樣用jquery來實現這個模擬效果。
實現這個模擬效果,頁面的一般呼叫方式:
$('input').placeholder();
#首先,先寫jquery外掛的一般結構:
;(function($){ $.fn.placeholder = function(){//实现placeholder的代码 } })(jQuery)
下面我們就要判斷瀏覽器是否支援placeholder屬性
。
;(function($){ $.fn.placeholder = function(){this.each(function(){var _this = this;var supportPlaceholder = 'placeholder' in document.createElement('input');if(!supportPlaceholder){//不支持placeholder属性的操作} }); } })(jQuery)
我們要支援鍊式操作,如下:
;(function($){ $.fn.placeholder = function(){ return this.each(function(){var _this = this;var supportPlaceholder = 'placeholder' in document.createElement('input');if(!supportPlaceholder){//不支持placeholder属性的操作} }); } })(jQuery)
預設組態項目:
options = $.extend({ placeholderColor:'#aaaaaa', isSpan:false, //是否使用插入span标签模拟placeholder的方式,默认是不需要onInput:true //实时监听输入框},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標籤來模擬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});
以上是jQuery封裝placeholder的實例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

jQuery引用方法詳解:快速上手指南jQuery是一個受歡迎的JavaScript庫,被廣泛用於網站開發中,它簡化了JavaScript編程,並為開發者提供了豐富的功能和特性。本文將詳細介紹jQuery的引用方法,並提供具體的程式碼範例,幫助讀者快速上手。引入jQuery首先,我們需要在HTML檔案中引入jQuery函式庫。可以透過CDN連結的方式引入,也可以下載

jQuery中如何使用PUT請求方式?在jQuery中,發送PUT請求的方法與發送其他類型的請求類似,但需要注意一些細節和參數設定。 PUT請求通常用於更新資源,例如更新資料庫中的資料或更新伺服器上的檔案。以下是在jQuery中使用PUT請求方式的具體程式碼範例。首先,確保引入了jQuery庫文件,然後可以透過以下方式發送PUT請求:$.ajax({u

標題:jQuery小技巧:快速修改頁面所有a標籤的文字在網頁開發中,我們經常需要對頁面中的元素進行修改和操作。使用jQuery時,有時候需要一次修改頁面中所有a標籤的文字內容,這樣可以節省時間和精力。以下將介紹如何使用jQuery快速修改頁面所有a標籤的文本,同時給出具體的程式碼範例。首先,我們需要引入jQuery庫文件,確保在頁面中引入了以下程式碼:<

jQuery如何移除元素的height屬性?在前端開發中,經常會遇到需要操作元素的高度屬性的需求。有時候,我們可能需要動態改變元素的高度,而有時候又需要移除元素的高度屬性。本文將介紹如何使用jQuery來移除元素的高度屬性,並提供具體的程式碼範例。在使用jQuery操作高度屬性之前,我們首先需要了解CSS中的height屬性。 height屬性用於設定元素的高度

標題:使用jQuery修改所有a標籤的文字內容jQuery是一款受歡迎的JavaScript庫,被廣泛用於處理DOM操作。在網頁開發中,經常會遇到需要修改頁面上連結標籤(a標籤)的文字內容的需求。本文將介紹如何使用jQuery來實現這個目標,並提供具體的程式碼範例。首先,我們需要在頁面中引入jQuery庫。在HTML檔案中加入以下程式碼:

jQuery是一種流行的JavaScript庫,被廣泛用於處理網頁中的DOM操作和事件處理。在jQuery中,eq()方法是用來選擇指定索引位置的元素的方法,具體使用方法和應用場景如下。在jQuery中,eq()方法選擇指定索引位置的元素。索引位置從0開始計數,即第一個元素的索引是0,第二個元素的索引是1,依此類推。 eq()方法的語法如下:$("s

如何判斷jQuery元素是否具有特定屬性?在使用jQuery操作DOM元素時,常會遇到需要判斷元素是否具有某個特定屬性的情況。在這種情況下,我們可以藉助jQuery提供的方法來輕鬆實現這項功能。以下將介紹兩種常用的方法來判斷一個jQuery元素是否具有特定屬性,並附上具體的程式碼範例。方法一:使用attr()方法和typeof運算子//判斷元素是否具有特定屬

近日消息,Mozilla在發布Firefox112穩定版的同時,也宣布下個主要版本Firefox113進入Beta頻道,支援AV1動圖、增強密碼產生器和畫中畫特性。火狐瀏覽器Firefox113主要新功能/新特性如下支援AV1格式動圖(AVIS)透過引入特殊字元來增強密碼產生器的安全性增強畫中畫功能,支援後退、顯示影片時間,能更輕鬆地啟用全螢幕模式為Debian和Ubuntu發行版提供官方DEB安裝檔更新書籤導入功能,預設支援導入書籤的圖示在支援的硬體上預設啟用硬體加速AV1視訊解碼使用w
