1 //数组去重 利用了原型,对象字面量 2 Array.prototype.unique = function() { 3 var newArray = [], temp = {}; 4 for (var i = 0, len = this.length; i < len; i++) { 5 temp[typeof(this[i]) + this[i]] = this[i]; 6 } 7 for (var item in temp) { 8 newArray.push(temp[item]); 9 } 10 return newArray; 11 }; 12 13
1 //去除字符串前后空格 2 String.prototype.trim=function(){ 3 return this.replace(/(^\s*)|(\s*$)/g, ""); 4 };
14 //textarea域自动扩展 需搭配jquery下使用 15 (function($) { 16 // jQuery plugin definition 17 $.fn.TextAreaExpander = function(minHeight, maxHeight) { 18 var hCheck = !($.browser.msie || $.browser.opera); 19 // resize a textarea 20 function ResizeTextarea(e) { 21 // event or initialize element? 22 e = e.target || e; 23 // find content length and box width 24 var vlen = e.value.length, ewidth = e.offsetWidth; 25 if (vlen != e.valLength || ewidth != e.boxWidth) { 26 if (hCheck && (vlen < e.valLength || ewidth != e.boxWidth)) e.style.height = "0px"; 27 var h = Math.max(e.expandMin, Math.min(e.scrollHeight, e.expandMax)); 28 e.style.overflow = (e.scrollHeight > h ? "auto" : "hidden"); 29 e.style.height = h + 5 + "px"; 30 e.valLength = vlen; 31 e.boxWidth = ewidth; 32 } 33 return true; 34 }; 35 // initialize 36 this.each(function() { 37 // is a textarea? 38 if (this.nodeName.toLowerCase() != "textarea") return; 39 // set height restrictions 40 var p = this.className.match(/expand(\d+)\-*(\d+)*/i); 41 this.expandMin = minHeight || (p ? parseInt('0'+p[1], 10) : 0); 42 this.expandMax = maxHeight || (p ? parseInt('0'+p[2], 10) : 99999); 43 // initial resize 44 ResizeTextarea(this); 45 // zero vertical padding and add events 46 if (!this.Initialized) { 47 this.Initialized = true; 48 $(this).css("padding-top", 0).css("padding-bottom", 0); 49 $(this).bind("keyup", ResizeTextarea).bind("focus", ResizeTextarea); 50 } 51 }); 52 return this; 53 }; 54 })(jQuery); 55 // initialize all expanding textareas 56 jQuery(document).ready(function() { 57 jQuery("textarea[class*=expand]").TextAreaExpander(); 58 });