Jquery实现<textarea>根据内容自动改变大小
2014-10-15 10:13
656 查看
<script type="text/javascript" src="${webRoot}/js/jquery.autogrow.textarea.js"></script>
<h3 >任职要求</h3>
<div> <textarea style="outline:none;resize : none; BORDER-BOTTOM: 0px solid; BORDER-LEFT: 0px solid; BORDER-RIGHT: 0px solid; BORDER-TOP: 0px solid;overflow-y:hidden" cols="120" rows="10" >${回显}</textarea></div>
jquery.autogrow.textarea.js如下:
/*
Auto-growing textareas; technique ripped from Facebook
(Textarea need set style "overflow:hidden" under IE)
*/
(function($) {
function times(string, number) {
for (var i = 0, r = ''; i < number; i ++) r += string;
return r;
}
$.fn.autogrow = function(options) {
this.filter('textarea').each(function() {
this.timeoutId = null;
var $this = $(this), minHeight = $this.height();
var shadow = $('<div></div>').css({
position: 'absolute',
wordWrap: 'break-word',
top: 0,
left: -9999,
display: 'none',
width: $this.width(),
fontSize: $this.css('fontSize'),
fontFamily: $this.css('fontFamily'),
lineHeight: $this.css('lineHeight')
}).appendTo(document.body);
var update = function() {
var val = this.value.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/&/g, '&')
.replace(/\n$/, '<br/> ')
.replace(/\n/g, '<br/>')
.replace(/ {2,}/g, function(space) { return times(' ', space.length -1) + ' ' });
shadow.html(val);
$(this).css('height', Math.max(shadow.height(), minHeight));
}
var updateTimeout = function() {
clearTimeout(this.timeoutId);
var that = this;
this.timeoutId = setTimeout(function(){ update.apply(that); }, 100);
};
$(this).change(update).keyup(updateTimeout).keydown(updateTimeout);
update.apply(this);
});
return this;
}
})(jQuery);
<h3 >任职要求</h3>
<div> <textarea style="outline:none;resize : none; BORDER-BOTTOM: 0px solid; BORDER-LEFT: 0px solid; BORDER-RIGHT: 0px solid; BORDER-TOP: 0px solid;overflow-y:hidden" cols="120" rows="10" >${回显}</textarea></div>
jquery.autogrow.textarea.js如下:
/*
Auto-growing textareas; technique ripped from Facebook
(Textarea need set style "overflow:hidden" under IE)
*/
(function($) {
function times(string, number) {
for (var i = 0, r = ''; i < number; i ++) r += string;
return r;
}
$.fn.autogrow = function(options) {
this.filter('textarea').each(function() {
this.timeoutId = null;
var $this = $(this), minHeight = $this.height();
var shadow = $('<div></div>').css({
position: 'absolute',
wordWrap: 'break-word',
top: 0,
left: -9999,
display: 'none',
width: $this.width(),
fontSize: $this.css('fontSize'),
fontFamily: $this.css('fontFamily'),
lineHeight: $this.css('lineHeight')
}).appendTo(document.body);
var update = function() {
var val = this.value.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/&/g, '&')
.replace(/\n$/, '<br/> ')
.replace(/\n/g, '<br/>')
.replace(/ {2,}/g, function(space) { return times(' ', space.length -1) + ' ' });
shadow.html(val);
$(this).css('height', Math.max(shadow.height(), minHeight));
}
var updateTimeout = function() {
clearTimeout(this.timeoutId);
var that = this;
this.timeoutId = setTimeout(function(){ update.apply(that); }, 100);
};
$(this).change(update).keyup(updateTimeout).keydown(updateTimeout);
update.apply(this);
});
return this;
}
})(jQuery);
相关文章推荐
- Jquery实现 TextArea 文本框根据输入内容自动适应高度
- 在.Net的WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变<收藏>
- Jquery实现 TextArea 文本框根据输入内容自动适应高度
- flex布局无法自动适应的bug以及实现textarea根据内容自适应
- jquery实现根据浏览器窗口大小自动缩放图片的方法
- Jquery实现textarea根据文本内容自适应高度
- HTML页面加载完后,根据内容调整<textarea>元素的高度
- jQuery实现textarea高度根据内容自适应
- java web开发在web.xml配置<mime-mapping>实现浏览器根据文件类型自动打开
- 【转】如何实现<textarea> placeholder自动换行?
- html 中用 Jquery实现 <textarea /> 中的 maxlength属性
- Extjs textarea 实现 数据框根据内容自动扩张
- jquery实现根据浏览器窗口大小自动缩放图片的方法
- 在 WEB.XML配置<mime-mapping>实现浏览器根据文件类型自动打开
- C#实现根据图片的EXIF自动调整图片方向<转>
- 在 web.xml中配置<mime-mapping>实现浏览器根据文件类型自动打开
- #269 – 根据内容自动改变窗口的大小(Automatically Sizing a Window to Fit Its Contents)
- jQuery实现<textarea>高度自适应,适用于td中的textarea
- js实现textarea根据内容大小自适应高度
- 关于textarea根据文本内容自动改变高度