---
title: "textarea文本域自适应内容高度"
author: "bryant513"
date: "2012-08-27T11:50:22Z"
tags: []
categories: ["Web前端"]
slug: "textarea"
draft: false
lastmod: "2012-08-27T11:50:22Z"
id: 1663762
url: "https://bryant513.iteye.com/blog/1663762"
---

原先自己写的一个方法，在textarea中用onpropertychange和oninput里监听调用

&lt;script&gt;

  function adjustHeight(blogId){

    var element = $("#comment\_content\_"+blogId);

    var minLimit = 24;

    if (!$.browser.mozilla){

      minLimit  += 4;

    }

    if (element\[0].scrollHeight &lt;= minLimit){

      element.css('height', ""+24);

      return false;

    }

    var newHeight = element\[0].scrollHeight;

    newHeight = newHeight - 10;

    element.css('height', newHeight);

  } 

&lt;/script&gt;

 

后来发现国外有人写好的jquery插件，代码量很少，功能也比较完善

http://bgrins.github.com/ExpandingTextareas/