[jQuery] textarea 自動調整高度

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>textarea auto height</title>
    <style type="text/css">
    textarea {
        resize: none;
    }
    </style>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        $("textarea.auto-height").css("overflow", "hidden").bind("keydown keyup", function() {
            $(this).height('0px').height($(this).prop("scrollHeight") + 'px');
        }).keydown();
    });
    </script>
</head>
<body>
    <textarea class="auto-height"></textarea>
</body>
</html>
Licensed under CC BY-NC-SA 3.0 TW
comments powered by Disqus