• [转]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 });
  • 相关阅读:
    windows下python3.6环境搭建
    接口自动化(2)----如何编写接口自动化用例
    关于接口自动化测试的规则说明
    linux 常用命令 补充
    linux 常用命令
    linux 初识
    java高级教程 JDK代理和CGLIB代理两种方式 账户类
    java高级教程 实例化和非实例化 bean 学生信息
    java高级教程 俩数之和
    C++使用printf输出string类
  • 原文地址:https://www.cnblogs.com/guojian2080/p/3031095.html
Copyright © 2020-2023  润新知