//需要jQuery
<textarea id="test"></textarea>
<script src="./libs/jquery.min.js"></script>
<script>
(function ($) {
$.fn.autotextarea = function (options) {
var defaults = {
maxHeight: null,
minHeight: $(this).height()
}
var opts = $.extend({}, defaults, options)
return $(this).each(function () {
$(this).bind('paste cut keydown keyup focus blur', function () {
var height, style = this.style;
this.style.height = opts.minHeight + "px";
if (this.scrollHeight > opts.minHeight) {
if (opts.max && this.scrollHeight > opts.maxHeight) {
height = opts.maxHeight;
style.oveflowY = "scroll";
} else {
height = this.scrollHeight;
style.oveflowY = "hidden";
}
style.height = height + "px"
} else {
}
})
})
}
})(jQuery)
</script>
<script>
$("#test").autotextarea({
maxHeight: 300,
minHeight: 40,
})
</script>