资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Jquery实现<textarea>根据内容自动改变大小

创新互联建站长期为上1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为法库企业提供专业的网站制作、成都网站建设,法库网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。


任职要求

jquery.autogrow.textarea.js如下:

/*

Auto-growing textareas; technique ripped from Facebook

(Textarea need set style "overflow:hidden" under IE)

*/

(function($) {

function times(string, number) {

  for (var i = 0, r = ''; i < number; i ++) r += string;

  return r;

}

 

$.fn.autogrow = function(options) {

  this.filter('textarea').each(function() {

    this.timeoutId = null;

    var $this = $(this), minHeight = $this.height();

    var shadow = $('

').css({

      position:   'absolute',

      wordWrap:   'break-word',

      top:        0,

      left:       -9999,

      display:    'none',

      width:      $this.width(),

      fontSize:   $this.css('fontSize'),

      fontFamily: $this.css('fontFamily'),

      lineHeight: $this.css('lineHeight')

    }).appendTo(document.body);

 

    var update = function() {

      var val = this.value.replace(/

        .replace(/>/g, '>')

        .replace(/&/g, '&')

        .replace(/\n$/, '
 ')

        .replace(/\n/g, '
')

        .replace(/ {2,}/g, function(space) { return times(' ', space.length -1) + ' ' });

      shadow.html(val);

      $(this).css('height', Math.max(shadow.height(), minHeight));

    }

     

    var updateTimeout = function() {

      clearTimeout(this.timeoutId);

      var that = this;

      this.timeoutId = setTimeout(function(){ update.apply(that); }, 100);

    };

 

    $(this).change(update).keyup(updateTimeout).keydown(updateTimeout);

    update.apply(this);

  });

  return this;

}

})(jQuery);



本文题目:Jquery实现<textarea>根据内容自动改变大小
本文网址:http://cdkjz.cn/article/jdiejs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220