小編給大家分享一下jQuery如何封裝placeholder效果,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)是一家以網(wǎng)站設(shè)計(jì)建設(shè),成都小程序開(kāi)發(fā)、網(wǎng)站開(kāi)發(fā)設(shè)計(jì),網(wǎng)絡(luò)軟件產(chǎn)品開(kāi)發(fā),企業(yè)互聯(lián)網(wǎng)推廣服務(wù)為主的民營(yíng)科技公司。主要業(yè)務(wù)涵蓋:為客戶(hù)提供網(wǎng)站策劃、網(wǎng)站設(shè)計(jì)、網(wǎng)站開(kāi)發(fā)、國(guó)際域名空間、網(wǎng)站優(yōu)化排名、買(mǎi)鏈接等服務(wù)領(lǐng)域。憑借建站老客戶(hù)口碑做市場(chǎng),建設(shè)網(wǎng)站時(shí),根據(jù)市場(chǎng)搜索規(guī)律和搜索引擎的排名收錄規(guī)律編程,全力為建站客戶(hù)設(shè)計(jì)制作排名好的網(wǎng)站,深受老客戶(hù)認(rèn)可和贊譽(yù)。
頁(yè)面中的輸入框默認(rèn)的提示文字一般使用placeholder屬性就可以了,即:
<input type="text" name="username" placeholder="請(qǐng)輸入用戶(hù)名" value="" id="username"/>
最多加點(diǎn)樣式控制下默認(rèn)文字的顏色
input::-webkit-input-placeholder{color:#AAAAAA;}但是在低版本的瀏覽器卻不支持這個(gè)placeholder屬性,那么真的要在低版本瀏覽器也要實(shí)現(xiàn)跟placeholder一樣的效果,就需要寫(xiě)個(gè)插件來(lái)兼容下,下面就細(xì)講一下怎樣用jquery來(lái)實(shí)現(xiàn)這個(gè)模擬效果。
實(shí)現(xiàn)這個(gè)模擬效果,頁(yè)面的一般調(diào)用方式:
$('input').placeholder();首先,先寫(xiě)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)聽(tīng)輸入框
},options);如果不需要通過(guò)span來(lái)模擬placeholder效果,那么就需要通過(guò)輸入框的value值來(lái)判斷,如下代碼:
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)簽來(lái)模擬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')
});
//通過(guò)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)聽(tīng)輸入框
},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')
});
//通過(guò)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)用方式,需要通過(guò)span標(biāo)簽來(lái)模擬的話(huà):
$("#username").placeholder({
isSpan:true
});以上是“jQuery如何封裝placeholder效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
新聞名稱(chēng):jQuery如何封裝placeholder效果
文章地址:http://chinadenli.net/article44/gsgghe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站維護(hù)、網(wǎng)站制作、企業(yè)網(wǎng)站制作、網(wǎng)站策劃、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)