textarea按照普通元素设置height是没有做用的,能够这么来设置,javascript
下面给上一段项目代码css
JS代码:java
$.fn.extend({ txtaAutoHeight: function () { return this.each(function () { var $this = $(this); if (!$this.attr('initAttrH')) { $this.attr('initAttrH', $this.outerHeight()); } setAutoHeight(this).on('input', function () { setAutoHeight(this); }); }); function setAutoHeight(elem) { var $obj = $(elem); //让textArea自动调节高度,使其不出现滚动条 // return $obj.css({ height: $obj.attr('initAttrH'), 'overflow-y': 'hidden' }).height(elem.scrollHeight); //让textArea自动适应父元素的高度 var $parentDom = $obj.parent(); return $obj.css({ height: $obj.attr('initAttrH'), 'overflow-y': 'hidden' }).height($parentDom.height()); } } }); /** * 动态textArea祖先元素节点的高度,myCener是整个页面高度,otherDiv是textArea的祖先元素之外的其余高度, * 利用(总高度-其余元素高度),来设置textArea祖先元素的高度 * @returns */ function setAutoTextAreaHeightFun(){ var totalHeight = $('#myCener').height(); var height1 = 0; if(!$('#otherDiv').is(":hidden")){ height1 = $('#otherDiv').height(); } var textAreaHeight = totalHeight - height1; $('.textAreaParent').height(textAreaHeight-8); }