[转]js使用技术二则(textarea域自动扩展及数组去重)

 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 });

 

 

转载于:https://www.cnblogs.com/guojian2080/archive/2013/04/19/3031095.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值