~function() { $.uiparse = function(action) { var arr = action.split('|').slice(1) var len = arr.length var res = [], exs var boo = /^(true|false)$/ for (var i = 0; i < len; i++) { var item = arr[i] if (item == '&') { item = undefined } else if (exs = item.match(boo)) { item = exs[0] == 'true' ? true : false } res[i] = item } return res }; /* * 璁剧疆杈撳叆鍩?input/textarea)鍏夋爣鐨勪綅缃? * @param {number} index */ $.fn.setcursorposition = function(option) { var settings = $.extend({ index: 0 }, option) return this.each(function() { var elem = this var val = elem.value var len = val.length var index = settings.index // 闈瀒nput鍜宼extarea鐩存帴杩斿洖 var $elem = $(elem) if (!$elem.is('input,textarea')) return // 瓒呰繃鏂囨湰闀垮害鐩存帴杩斿洖 if (len < index) return settimeout(function() { elem.focus() if (elem.setselectionrange) { // 鏍囧噯娴忚鍣? elem.setselectionrange(index, index) } else { // ie9- var range = elem.createtextrange() range.movestart("character", -len) range.moveend("character", -len) range.movestart("character", index) range.moveend("character", 0) range.select() } }, 10) }) }; /** * placeholder缁勪欢 * $(input).focuspic({ * word: // @string 鎻愮ず鏂囨湰 * color: // @string 鏂囨湰棰滆壊 * evttype: // @string focus|keydown 瑙﹀彂placeholder鐨勪簨浠剁被鍨? * }) * * note锛? * evttype榛樿鏄痜ocus锛屽嵆榧犳爣鐐瑰嚮鍒拌緭鍏ュ煙鏃堕粯璁ゆ枃鏈秷澶憋紝keydown鍒欐ā鎷烪tml5 placeholder灞炴€у湪firefox/chrome閲岀殑鐗瑰緛锛屽厜鏍囧畾浣嶅埌杈撳叆鍩熷悗閿洏杈撳叆鏃堕粯璁ゆ枃鏈墠娑堝け銆? * 姝ゅ锛屽浜嶩tml5 placeholder灞炴€э紝ie10+鍜孎irefox/chrome/safari鐨勮〃鐜板舰寮忎篃涓嶄竴鑷达紝鍥犳鍐呴儴瀹炵幇涓嶉噰鐢ㄥ師鐢焢laceholder灞炴€? */ $.fn.placeholder = function(option, callback) { var settings = $.extend({ word: '', color: '#999999', evttype: 'focus' }, option) function bootstrap($that) { // some alias var word = settings.word var color = settings.color var evttype = settings.evttype // default var defcolor = $that.css('color') var defval = $that.val() if (defval == '' || defval == word) { $that.css({color: color}).val(word) } else { $that.css({color: defcolor}) } function switchstatus(isdef) { if (isdef) { $that.val('').css({color: defcolor}) } else { $that.val(word).css({color: color}) } } function asfocus() { $that.bind(evttype, function() { var txt = $that.val() if (txt == word) { switchstatus(true) } }).bind('blur', function() { var txt = $that.val() if (txt == '') { switchstatus(false) } }) } function askeydown() { $that.bind('focus', function() { var elem = $that[0] var val = $that.val() if (val == word) { settimeout(function() { // 鍏夋爣瀹氫綅鍒伴浣? $that.setcursorposition({index: 0}) }, 10) } }) } if (evttype == 'focus') { asfocus() } else if (evttype == 'keydown') { askeydown() } // keydown浜嬩欢閲屽鐞唒laceholder $that.keydown(function() { var val = $that.val() if (val == word) { switchstatus(true) } }).keyup(function() { var val = $that.val() if (val == '') { switchstatus(false) $that.setcursorposition({index: 0}) } }) } return this.each(function() { var $elem = $(this) bootstrap($elem) if ($.isfunction(callback)) callback($elem) }) } /* * 鑷姩鍒濆鍖栵紝閰嶇疆鍙傛暟鎸夌収浣跨敤棰戠巼鍏堝悗鎺掑簭锛屽嵆鏈€缁忓父浣跨敤鐨勫湪鍓嶏紝涓嶇粡甯镐娇鐢ㄧ殑寰€鍚庯紝浣跨敤榛樿鍙傛暟鏇夸唬 * * 鏍煎紡锛歞ata-ui="u-placeholder|word|evttype|color * 绀轰緥锛歞ata-ui="u-placeholder|榛樿鏂囧瓧 * */ $(function() { $('[data-ui^="u-placeholder"]').each(function() { var $elem = $(this) var arr = $.uiparse($elem.attr('data-ui')) // 鏂囨湰 var word = arr[0] // 浜嬩欢 var evttype = arr[1] // 鏂囨湰棰滆壊 var color = arr[2] // create $elem.placeholder({ word: word, color: color, evttype: evttype }) }) }) }();