您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

jquery实现textarea 高度自适应_jquery

2025/8/2 15:10:34发布24次查看
之前给大家分享过用javascript控制文本框textarea高度随内容自适应增长收缩,今天花了点时间换了种实现方法,总结一下
复制代码 代码如下:
jquery.fn.extend({
            autoheight: function(){
                return this.each(function(){
                    var $this = jquery(this);
                    if( !$this.attr('_initadjustheight') ){
                        $this.attr('_initadjustheight', $this.outerheight());
                    }
                    _adjusth(this).on('input', function(){
                        _adjusth(this);
                    });
                });
                /**
                 * 重置高度
                 * @param {object} elem
                 */
                function _adjusth(elem){
                    var $obj = jquery(elem);
                    return $obj.css({height: $obj.attr('_initadjustheight'), 'overflow-y': 'hidden'})
                            .height( elem.scrollheight );
                }
            }
        });
        // 使用
        $(function(){
            $('textarea').autoheight();
        });
以上就是本文所述的全部内容了,希望对大家学习jquery能够有所帮助。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product