jquery文字框textarea自適應高度

來源:互聯網
上載者:User

標籤:style   blog   ar   io   sp   for   java   on   div   

瀏覽器中預設的文字框是不能根據內容的增多變高,只能固定高度有捲軸,體驗不是很好,找了很多方法相容都不行,總算找到個相容良好的方法:

<body>        <textarea id="textarea3" style="overflow-y:hidden; height:20px;resize: none">                </textarea>        <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>        <script type="text/javascript">            $(function() {                //最小高度和最大高度預設                $("#textarea1").textareaAutoHeight();                //最大高度為100px                $("#textarea2").textareaAutoHeight({maxHeight: 100});                //最小高度為50px,最大高度為200px                $("#textarea3").textareaAutoHeight({minHeight: 50, maxHeight: 200});            })            $.fn.extend({                textareaAutoHeight: function(options) {                    this._options = {                        minHeight: 0,                        maxHeight: 1000                    }                    this.init = function() {                        for (var p in options) {                            this._options[p] = options[p];                        }                        if (this._options.minHeight == 0) {                            this._options.minHeight = parseFloat($(this).height());                        }                        for (var p in this._options) {                            if ($(this).attr(p) == null) {                                $(this).attr(p, this._options[p]);                            }                        }                        $(this).keyup(this.resetHeight).change(this.resetHeight)                                .focus(this.resetHeight);                    }                    this.resetHeight = function() {                        var _minHeight = parseFloat($(this).attr("minHeight"));                        var _maxHeight = parseFloat($(this).attr("maxHeight"));                        if (!$.browser.msie) {                            $(this).height(0);                        }                        var h = parseFloat(this.scrollHeight);                        h = h < _minHeight ? _minHeight :h > _maxHeight ? _maxHeight : h;                        $(this).height(h).scrollTop(h);                        if (h >= _maxHeight) {                            $(this).css("overflow-y", "scroll");                        }                        else {                            $(this).css("overflow-y", "hidden");                        }                    }                    this.init();                }            });        </script>    </body>

 

jquery文字框textarea自適應高度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.