Jquery Textarea自动增长插件。如何改进?
我在网上找到了这段代码,并根据我的需要对其进行了修改。它是一个文本区域,根据字符输入进行扩展和收缩,一旦用户按Enter键,它就会插入注释 我想知道是否有任何方法可以改进它,使它更优雅等等。如果这是使用最新版本的JQuery编写的,那么会添加或删除什么 多谢各位 代码Jquery Textarea自动增长插件。如何改进?,jquery,Jquery,我在网上找到了这段代码,并根据我的需要对其进行了修改。它是一个文本区域,根据字符输入进行扩展和收缩,一旦用户按Enter键,它就会插入注释 我想知道是否有任何方法可以改进它,使它更优雅等等。如果这是使用最新版本的JQuery编写的,那么会添加或删除什么 多谢各位 代码 jQuery.fn.autoGrow = function(){ return this.each(function(){ // Variables var colsDefault =
jQuery.fn.autoGrow = function(){
return this.each(function(){
// Variables
var colsDefault = this.cols;
var rowsDefault = this.rows;
//Functions
var grow = function() {
growByRef(this);
}
var onFocus = function(obj) {
if ($(this).val() != 'Write a comment...') {
return;
} else {
$(this).parents(".comment_new_container").children("img").show();
//$(this).height(34);
$(this).width(742);
$(this).val('');
}
}
var onBlur = function(obj) {
if ($(this).val().length == 0) {
$(this).parents(".comment_new_container").children("img").hide();
//$(this).height(16);
$(this).width(792);
$(this).val('Write a comment...');
}
}
var growByRef = function(obj) {
var new_comment = '';
if (!obj.shiftKey && obj.keyCode == 13) {
obj.preventDefault();
new_comment += '<div class="comment_container" id="001">';
new_comment += '<a href="#"><i class="comment_delete"> </i></a>';
new_comment += '<img src="img/avatar45.png" />';
new_comment += '<a href="/">Mickey Mouse</a>';
new_comment += '<br/>';
new_comment += '<div class="comment_content">' + $(this).val().replace(/\n/g, '<br />'); + '</div> <!-- End comment_content -->';
new_comment += '<div class="comment_timestamp">13 minutes ago</div> <!-- End comment_timestamp -->';
new_comment += '</div> <!-- End comment_container -->';
$(new_comment).insertBefore($(this).parents(".comment_new_container"));
var comment_total = parseInt($(this).parents('.entry_container').find('.meta.comment_total').text(), 10) + 1;
$(this).parents('.entry_container').find('.meta.comment_total').text(comment_total);
$(this).val('Write a comment...');
$(this).blur();
growByRef(this);
} else {
var linesCount = 0;
var lines = obj.value.split('\n');
for (var i=lines.length-1; i>=0; --i)
{
linesCount += Math.floor((lines[i].length / colsDefault) + 1);
}
if (linesCount >= rowsDefault) {
obj.rows = linesCount + 1;
} else {
obj.rows = rowsDefault;
}
}
}
var characterWidth = function (obj){
var characterWidth = 0;
var temp1 = 0;
var temp2 = 0;
var tempCols = obj.cols;
obj.cols = 1;
temp1 = obj.offsetWidth;
obj.cols = 2;
temp2 = obj.offsetWidth;
characterWidth = temp2 - temp1;
obj.cols = tempCols;
return characterWidth;
}
// Manipulations
$(this).css("width","auto");
$(this).css("height","auto");
$(this).css("overflow","hidden");
this.style.width = ((characterWidth(this) * this.cols) + 6) + "px";
$(this).bind("focus", onFocus);
$(this).bind("blur", onBlur);
$(this).bind("keypress", growByRef);
$(this).bind("keyup", grow);
});
};
jQuery.fn.autoGrow=function(){
返回此值。每个(函数(){
//变数
var colsDefault=this.cols;
var rowsDefault=this.rows;
//功能
var grow=function(){
growByRef(这个);
}
var onFocus=功能(obj){
如果($(this.val()!='写评论…')){
返回;
}否则{
$(this).parents(“.comment\u new\u container”).children(“img”).show();
//$(此)。高度(34);
美元(此)。宽度(742);
$(this.val(“”);
}
}
var onBlur=功能(obj){
如果($(this.val().length==0){
$(this).parents(“.comment\u new\u container”).children(“img”).hide();
//$(此)。高度(16);
美元(此)。宽度(792);
$(this.val('Write a comment…');
}
}
var growByRef=函数(obj){
var new_comment='';
如果(!obj.shiftKey&&obj.keyCode==13){
obj.preventDefault();
新的_评论+='';
新的_评论+='';
新的_评论+='';
新的_评论+='';
新注释+='
';
新建注释+=''+$(this.val()。替换(/\n/g,
);+';
新的_评论+=‘13分钟前’;
新的_评论+='';
$(new\u comment).insertBefore($(this.parents(.comment\u new\u container));
var comment_total=parseInt($(this).parents('.entry_container').find('.meta.comment_total').text(),10)+1;
$(this).parents('.entry_container').find('.meta.comment_total').text(comment_total);
$(this.val('Write a comment…');
$(this.blur();
growByRef(这个);
}否则{
var-linescont=0;
变量行=对象值分割('\n');
对于(变量i=lines.length-1;i>=0;--i)
{
lineScont+=Math.floor((行[i].length/colsDefault)+1);
}
如果(lineScont>=行故障){
obj.rows=lineScont+1;
}否则{
obj.rows=rowsDefault;
}
}
}
变量characterWidth=函数(obj){
var characterWidth=0;
var-temp1=0;
var-temp2=0;
var tempCols=obj.cols;
obj.cols=1;
temp1=对象偏移网络宽度;
obj.cols=2;
temp2=对象偏移网络宽度;
characterWidth=temp2-temp1;
obj.cols=临时工;
返回字符宽度;
}
//操纵
$(this.css(“宽度”、“自动”);
$(this.css(“高度”、“自动”);
$(this.css(“溢出”、“隐藏”);
this.style.width=((characterWidth(this)*this.cols)+6)+“px”;
$(this.bind(“focus”,onFocus);
$(this.bind(“blur”,onBlur);
$(this.bind(“按键”,growByRef);
$(this.bind(“keyup”,grow);
});
};
你应该阅读,因为它们涵盖了基本内容。例如,您正在使用$
而不是jQuery
,但变量的名称可能已更改。您可以通过使用此闭包绕过此问题:
(function( $ ) {
$.fn.myPlugin = function() {
// Do your awesome plugin stuff here
};
})( jQuery );
隐马尔可夫模型。。。在文本区域中按enter键提交表单?我讨厌这样的表单。就最新的jquery而言,唯一的改变就是将
.bind
替换为.on
。您是否有此工作的工作演示,例如在JSFIDLE上?