欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

jquery 插件实现多行文本框[textarea]自动高度_jquery

程序员文章站 2022-05-17 16:24:36
...
实现功能:

1/当textarea换行时自动增加一行高度
2/当textarea删除一行时 自动减少一行的高度 依赖:jquery.xxx.js 工作中需要使用类似功能但是觉得使用插件需要导入其他文件很不方便所以就写了一个

textarea jquery插件

复制代码 代码如下:







复制代码 代码如下:

jQuery.extend({
textareaAutosize_dc: function() {
$("textarea").on("keyup", function(e) {
var currentEnterCount = $(this).val().split("\n").length;
var lineHeight = Number($(this).css("line-height").replace("px", ""));
var enterCount = $(this).attr("enterCount");
if (currentEnterCount //每行减掉固定行高
$(this).height($(this).height() - lineHeight);
} else if (currentEnterCount > enterCount) {
//每行加入固定行高
$(this).height($(this).height() + lineHeight);
$(this).attr("enterCount", currentEnterCount);
}
//记录当前行高
$(this).attr("enterCount", currentEnterCount);
});
}
});
//调用自动高度
$.textareaAutosize_dc();

以上就是本文的全部内容了,希望大家能够喜欢。