自适应高度的 textarea

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>自适应高度的textarea</title>
    <style>
        textarea { border: 1px solid gray; width: 100%; overflow: hidden; }
    </style>
    <script src="Scripts/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('textarea').bind('input propertychange focus', function () {
                this.style.posHeight = this.scrollHeight;
            });
        });
    </script>
</head>
<body>
    <textarea>
        测试1
        测试2
    </textarea>
</body>
</html>

HTML5支持input事件,IE9及以下的IE浏览器支持propertychange事件。

已知bug:在IE9中,使用鼠标右键剪切、粘贴操作文本时,不能触发事件。

posted on 2014-03-12 10:24  飞扬的尘埃  阅读(633)  评论(0编辑  收藏  举报

导航