jQuery實(shí)現(xiàn)的一個(gè)自定義Placeholder屬性插件
HTML5中文本框的新屬性placeholder是個(gè)非常好用的屬性,但是IE系列直至IE9都不支持這一屬性,這就讓大家在用這一屬性的時(shí)候有些猶豫不決。自己曾經(jīng)寫(xiě)過(guò)很多類似共的小控件,但是都不是很通用,這里分享一個(gè)漸進(jìn)增強(qiáng)的自定義placeholder的jQuery插件。有點(diǎn)是使用簡(jiǎn)單,大家也可以根據(jù)自己的需要進(jìn)行改進(jìn)。平常寫(xiě)jQuery插件比較少,考慮到用jQuery的同學(xué)比較多,這里就用jQuery插件的形式編寫(xiě)了。
在這里簡(jiǎn)單的介紹一下實(shí)現(xiàn)思路。
1.表現(xiàn)與html5原生的placeholder盡量類似
2.漸進(jìn)增強(qiáng)對(duì)于支持placeholder的瀏覽器不做處理
一、首先是幾個(gè)工具方法:
1.supportProperty(nodeType, property),獲取瀏覽器是否支持某一控件的某一屬性
2.getPositionInDoc(target, parent),獲取對(duì)象在文檔中的位置
3.$c,一個(gè)快速創(chuàng)建Dom對(duì)象的方法
這幾個(gè)工具方法都是一些比較常見(jiàn)通用的方法,如果你有自己的或者更合適的可以自行替換。
二、主體,CustomPlaceholder對(duì)象。這個(gè)對(duì)象主要是維護(hù)每一個(gè)文本框的信息,包括其位置,應(yīng)該顯示的提示信息等等,另外它還包含創(chuàng)建提示信息以及定位等方法以及對(duì)象的相應(yīng)事件。
事件主要是在initEvents函數(shù)中進(jìn)行的處理,這里特別要注意的是對(duì)提示信息事件的處理,當(dāng)提示信息被點(diǎn)擊時(shí)焦點(diǎn)應(yīng)該被重新定位到文本框。而文本框要處理的則是focus和blur事件。
$(self.hint).bind( 'click', function(e){
self.input.focus();
});
$(self.input).bind( 'focus', function(e){
self.hint.style.display = 'none';
});
$(self.input).bind( 'blur', function(e){
if(this.value == ''){
self.hint.style.display = 'inline';
}
});
CustomPlacehodler對(duì)象的兩個(gè)主要方法是createHintLabel(text, position)和position()。createHintLabel是用于創(chuàng)建提示信息的DOM對(duì)象并對(duì)其進(jìn)行定位,并返回這個(gè)對(duì)象。position方法用于強(qiáng)制對(duì)提示消息進(jìn)行重新定位。主要用于頁(yè)面大小改變的情況。這兩個(gè)方法的功能和實(shí)現(xiàn)都比較簡(jiǎn)單。
三、插件的功能實(shí)現(xiàn)部分。jQuery插件實(shí)現(xiàn)方式就不多說(shuō)了。這里首先進(jìn)行了能力檢測(cè),如果原生支持placeholder則直接返回。
if(supportProperty('input', 'placeholder')){
return;
}
接下來(lái)是根據(jù)選擇的input對(duì)象,生成相應(yīng)的CustomPlaceholder對(duì)象,保存在數(shù)組中,并獲取每個(gè)對(duì)象的提示信息的DOM對(duì)象,添加到容器中,最后將容器附加到body對(duì)象中。
var customPlaceholders = [];
if(this.length > 0){
var box = $c('div', 'dk_placeholderfixed_box');
for(var i = 0, len = this.length; i < len; i++){
var input = this[i];
customPlaceholders.push(new CustomPlaceholder(box, input, option));
}
document.body.appendChild(box);
}
最后還有一件比較重要的事情,為window對(duì)象綁定resize事件,當(dāng)window對(duì)象觸發(fā)resize事件時(shí)對(duì)所有的customPlacehoder對(duì)象進(jìn)行重新定位。
$(window).bind( 'resize', function(e){
for(var i = 0, len = customPlaceholders.length; i < len; i++){
var customPlaceholder = customPlaceholders[i];
customPlaceholder.position();
}
});
這個(gè)簡(jiǎn)單的小插件到這里就寫(xiě)完了。
插件源碼:
(function($){ var eles = { div: document.createElement('div'), ul: document.createElement('ul'), li: document.createElement('li'), span: document.createElement('span'), p: document.createElement('p'), a: document.createElement('a'), fragment: document.createDocumentFragment(), input: document.createElement('input') } var supportProperty = function(nodeType, property){ switch(arguments.length){ case 0: return false; case 1: var property = nodeType, nodeType = 'div'; property = property.split('.'); if(property.length == 1){ return typeof eles[nodeType][property[0]] !== 'undefined'; }else if(property.length == 2){ return typeof eles[nodeType][property[0]][property[1]] !== 'undefined'; } case 2: property = property.split('.'); if(property.length == 1){ return typeof eles[nodeType][property[0]] !== 'undefined'; }else if(property.length == 2){ return typeof eles[nodeType][property[0]][property[1]] !== 'undefined'; } return false; default: return false; } }; var getPositionInDoc = function(target, parent) { if (!target) { return null; } var left = 0, top = 0; do { left += target.offsetLeft || 0; top += target.offsetTop || 0; target = target.offsetParent; if(parent && target == parent){ break; } } while (target); return { left: left, top: top }; } var $c = function(tagName, id, className){ var ele = null; if(!eles[tagName]){ ele = eles[tagName] = document.createElement(tagName); }else{ ele = eles[tagName].cloneNode(true); } if(id){ ele.id = id; } if(className){ ele.className = className; } return ele; }; var CustomPlaceholder = function(box, input, option){ var self = this; var position = getPositionInDoc(input); self.input = input; self.option = {xOffset:0, yOffset:0}; for(var item in option){ self.option[item] = option[item]; } self.hint = self.createHintLabel(input.getAttribute('placeholder'), position); box.appendChild(self.hint); self.initEvents = function(){ $(self.hint).bind( 'click', function(e){ self.input.focus(); }); $(self.input).bind( 'focus', function(e){ self.hint.style.display = 'none'; }); $(self.input).bind( 'blur', function(e){ if(this.value == ''){ self.hint.style.display = 'inline'; } }); }; self.initEvents(); }; CustomPlaceholder.prototype = { createHintLabel: function(text, position){ var hint = $c('label'); hint.style.cusor = 'text'; hint.style.position = 'absolute'; hint.style.left = position.left + this.option.xOffset + 'px'; hint.style.top = position.top + this.option.yOffset + 'px'; hint.innerHTML = text; hint.style.zIndex = '9999'; return hint; }, position: function(){ var position = getPositionInDoc(this.input); this.hint.style.left = position.left + this.option.xOffset + 'px'; this.hint.style.top = position.top + this.option.yOffset + 'px'; } }; $.fn.placeholder = function(option){ if(supportProperty('input', 'placeholder')){ return; } var customPlaceholders = []; if(this.length > 0){ var box = $c('div', 'dk_placeholderfixed_box'); for(var i = 0, len = this.length; i < len; i++){ var input = this[i]; if($(input).is(':visible')){ customPlaceholders.push(new CustomPlaceholder(box, input, option)); } } document.body.appendChild(box); } $(window).bind( 'resize', function(e){ for(var i = 0, len = customPlaceholders.length; i < len; i++){ var customPlaceholder = customPlaceholders[i]; customPlaceholder.position(); } }); }; })(jQuery);
- jquery獲取自定義屬性(attr和prop)實(shí)例介紹
- jQuery中使用data()方法讀取HTML5自定義屬性data-*實(shí)例
- jquery 獲取自定義屬性(attr和prop)的實(shí)現(xiàn)代碼
- 使用jQuery獲取data-的自定義屬性
- jquery自定義屬性(類型/屬性值)
- jQuery Easyui Tabs擴(kuò)展根據(jù)自定義屬性打開(kāi)頁(yè)簽
- jquery操作HTML5 的data-*的用法實(shí)例分享
- html5的自定義data-*屬性和jquery的data()方法的使用示例
- jquery遍歷標(biāo)簽中自定義的屬性方法
- 用jquery獲取自定義的標(biāo)簽屬性的值簡(jiǎn)單實(shí)例
- jQuery判斷自定義屬性data-val用法示例
相關(guān)文章
基于jQuery的左右滾動(dòng)實(shí)現(xiàn)代碼
jQuery左右滾動(dòng),jquery中有自定義動(dòng)畫(huà)。要實(shí)現(xiàn)左右滾動(dòng)就是將Html標(biāo)簽的left值進(jìn)行加減。2010-12-12Jquery利用mouseenter和mouseleave實(shí)現(xiàn)鼠標(biāo)經(jīng)過(guò)彈出層且可以點(diǎn)擊
這篇文章主要介紹了Jquery利用mouseenter和mouseleave實(shí)現(xiàn)鼠標(biāo)經(jīng)過(guò)彈出層且可以點(diǎn)擊。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02jQuery easyui的validatebox校驗(yàn)規(guī)則擴(kuò)展及easyui校驗(yàn)框validatebox用法
這篇文章主要介紹了jQuery easyui的validatebox校驗(yàn)規(guī)則擴(kuò)展及easyui校驗(yàn)框validatebox用法的相關(guān)資料,需要的朋友可以參考下2016-01-01jQuery根據(jù)緯度經(jīng)度查看地圖處理程序
jQuery根據(jù)緯度經(jīng)度查看地圖處理程序如下在這里要注意js的引入順序,有需求的朋友可以參考下哈希望對(duì)你有所幫助2013-05-05jQuery中驗(yàn)證表單提交方式及序列化表單內(nèi)容的實(shí)現(xiàn)
之前項(xiàng)目中使用的表單提交方式,使用form()方法可以將提交事件脫離submit按鈕,綁定到任何事件中,下面有個(gè)不錯(cuò)的示例大家可以參考下2014-01-01jquery dataview數(shù)據(jù)視圖插件使用方法
這篇文章主要介紹了jquery dataview數(shù)據(jù)視圖插件使用方法,數(shù)據(jù)填充與視圖更新利器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12