为jquery弹性插件而活?
我使用jquery弹性插件来扩展文本框。 它工作得很好,但我想在一个用ajax添加到DOM的文本框中使用它,但不幸的是这个插件没有实时内置函数。
有办法解决这个问题吗?
http://www.unwrongest.com/projects/elastic/
如果您可以控制textarea创建,那么只需在textarea创建时调用.elastic()
:
// In whatever AJAX callback creates the textarea... var newTextarea = $(''); // Append the element to the DOM wherever it belongs... parentElement.append(newTextarea); // Add elastic behavior. newTextarea.elastic();
使用jQuery 1.4,您可以这样做:
$("textarea").live("focus", function() { $(this).elastic().die("focus"); });
jQuery 1.3.x不支持live()的焦点事件,所以它有点棘手:
$("textarea").live("keydown", elasticize).live("mousedown", elasticize); function elasticize() { $(this).elastic().die("keydown").die("mousedown"); }
die
调用是有弹性的,每个textarea只调用一次。
其他答案建议销毁焦点处理程序,但这可能会破坏其他依赖项。
相反,您可以使用class =“elastic”标记textarea。 弹性初始化后,您可以删除“弹性”类以避免重复。
$(document) .on('focus','textarea.elastic',function(e) { $(this).removeClass('elastic').elastic(); })