博客
关于我
jquery动态改变textarea高度,高度随着内容增加而自动变高,内容减少而自动变低
阅读量:294 次
发布时间:2019-03-03

本文共 635 字,大约阅读时间需要 2 分钟。

jQuery代码实现自适应高度 textarea

作为开发人员,以下 jQuery 实现可以帮助 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);
});
});
}
});

function _adjustH(elem) {

var $obj = jQuery(elem);
return $obj.css({
height: $obj.attr('_initAdjustHeight'),
'overflow-y': 'hidden'
}).height(elem.scrollHeight);
}

在项目中使用时,可通过以下方式调用:

$(function() {

$('textarea').autoHeight();
});

该实现通过设置初始高度并响应输入事件,确保 textarea 始终保持合适的尺寸。

转载地址:http://bxyl.baihongyu.com/

你可能感兴趣的文章
Objective-C实现qubit measure量子位测量算法(附完整源码)
查看>>
Objective-C实现Queue队列算法(附完整源码)
查看>>
Objective-C实现Queue队列算法(附完整源码)
查看>>
Objective-C实现quick select快速选择算法(附完整源码)
查看>>
Objective-C实现rabin-karp算法(附完整源码)
查看>>
Objective-C实现radians弧度制算法(附完整源码)
查看>>
Objective-C实现radianToDegree弧度到度算法(附完整源码)
查看>>
Objective-C实现radix sort基数排序算法(附完整源码)
查看>>
Objective-C实现rail fence围栏密码算法(附完整源码)
查看>>
Objective-C实现randomized heap随机堆算法(附完整源码)
查看>>
Objective-C实现rayleigh quotient瑞利商算法(附完整源码)
查看>>
Objective-C实现RC4加解密算法(附完整源码)
查看>>
Objective-C实现RC4加解密算法(附完整源码)
查看>>
Objective-C实现recursive bubble sor递归冒泡排序算法(附完整源码)
查看>>
Objective-C实现recursive insertion sort递归插入排序算法(附完整源码)
查看>>
Objective-C实现recursive quick sort递归快速排序算法(附完整源码)
查看>>
Objective-C实现RedBlackTree红黑树算法(附完整源码)
查看>>
Objective-C实现redis分布式锁(附完整源码)
查看>>
Objective-C实现regular-expression-matching正则表达式匹配算法(附完整源码)
查看>>
Objective-C实现relu线性整流函数算法(附完整源码)
查看>>