是否有用于文本字段的 jQuery 自动增长插件? [英] Is there a jQuery autogrow plugin for text fields?
本文介绍了是否有用于文本字段的 jQuery 自动增长插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我找到了各种插件来自动增长 textarea,但没有找到输入文本字段.有没有人知道有没有?
I have found various plugins for auto-growing a textarea, but not input text fields. Does anybody know if any exist?
推荐答案
这里有一个插件可以满足您的需求:
Here's a plugin that'll do what you're after:
编辑:我已根据 Mathias 的评论修复了该插件.:)
EDIT: I've fixed the plugin as per Mathias' comment. :)
在此处查看演示:http://jsfiddle.net/rRHY
See a demo here: http://jsfiddle.net/rRHzY
插件:
(function($){
$.fn.autoGrowInput = function(o) {
o = $.extend({
maxWidth: 1000,
minWidth: 0,
comfortZone: 70
}, o);
this.filter('input:text').each(function(){
var minWidth = o.minWidth || $(this).width(),
val = '',
input = $(this),
testSubject = $('<tester/>').css({
position: 'absolute',
top: -9999,
left: -9999,
width: 'auto',
fontSize: input.css('fontSize'),
fontFamily: input.css('fontFamily'),
fontWeight: input.css('fontWeight'),
letterSpacing: input.css('letterSpacing'),
whiteSpace: 'nowrap'
}),
check = function() {
if (val === (val = input.val())) {return;}
// Enter new content into testSubject
var escaped = val.replace(/&/g, '&').replace(/s/g,' ').replace(/</g, '<').replace(/>/g, '>');
testSubject.html(escaped);
// Calculate new width + whether to change
var testerWidth = testSubject.width(),
newWidth = (testerWidth + o.comfortZone) >= minWidth ? testerWidth + o.comfortZone : minWidth,
currentWidth = input.width(),
isValidWidthChange = (newWidth < currentWidth && newWidth >= minWidth)
|| (newWidth > minWidth && newWidth < o.maxWidth);
// Animate width
if (isValidWidthChange) {
input.width(newWidth);
}
};
testSubject.insertAfter(input);
$(this).bind('keyup keydown blur update', check);
});
return this;
};
})(jQuery);
这篇关于是否有用于文本字段的 jQuery 自动增长插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文