<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title></title>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script type="text/javascript">
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();
});
</script>
</head>
<body>
<textarea id="txt"></textarea>
</body>
</html>
上面代码完美实现高度自适应功能,更多内容参阅下面文章。
如果您觉得本文的内容对您的学习有所帮助:
关键字:
jQuery