开发者

live for jquery elastic plugin?

i use jquery elastic plugin for expanding a textbox. it works great but i want to use this on a textbox that is added to the DOM with ajax but unfortunately this plugin has no live开发者_JAVA技巧 built-in function.

is there a way to solve this?

http://www.unwrongest.com/projects/elastic/


If you have control over the textarea creation, then simply call .elastic() on the textarea once it is created:

// In whatever AJAX callback creates the textarea...
var newTextarea = $('<textarea></textarea>');

// Append the element to the DOM wherever it belongs...
parentElement.append(newTextarea);

// Add elastic behavior.
newTextarea.elastic();


With jQuery 1.4, you can do this:

$("textarea").live("focus", function() {
    $(this).elastic().die("focus");
});

jQuery 1.3.x does not support the focus event for live(), so it gets a little trickier:

$("textarea").live("keydown", elasticize).live("mousedown", elasticize);

function elasticize() {
    $(this).elastic().die("keydown").die("mousedown");
}

The die calls are there so elastic is only called once for each textarea.


The other answers suggest destroying the focus handler, but that may break other dependencies.

Instead, you can tag your textarea with class="elastic". Once elastic is initialized, you can remove the "elastic" class to avoid repeat inits.

$(document) .on('focus','textarea.elastic',function(e) { $(this).removeClass('elastic').elastic(); })

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜