また、一般的にライブjqueryプラグインを作成する方法を理解しようとしています.これは良い例だと思いました.
私が開発しようとしているこのプラグインは、テキストエリア用の autoGrow のようなもので、これまでのところうまく機能しますが、ライブ コンテンツでは機能しません。
http://jsfiddle.net/denislexic/cPBgG/10/
ライブではないためeach
だと思いますが、回避方法がわかりません。私は定期的にこの問題に遭遇してきましたが、解決策が見つかりません。
助けていただければ幸いです。
ここにコードがあります
$.fn.autogrow = function(options) {
this.filter('textarea').each(function() {
var $this = $(this),
minHeight = $this.height(),
lineHeight = $this.css('lineHeight');
$(this).css('height','hidden');
var duplicate = $('<div></div>').css({
position: 'absolute',
top: -10000,
left: -10000,
width: $(this).width(),
fontSize: $this.css('fontSize'),
fontFamily: $this.css('fontFamily'),
lineHeight: $this.css('lineHeight'),
resize: 'none'
}).appendTo(document.body);
var update = function() {
var val = this.value.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/&/g, '&')
.replace(/\n/g, '<br/>');
duplicate.html(val);
$(this).css('height', Math.max(duplicate.height() + 20, minHeight));
}
$(this).change(update).keyup(update).keydown(update);
update.apply(this);
});
return this;
}