2009-12-22 31 views

回答

4

如果你有超過textarea的創作控制,那麼只需在文本區域叫.elastic()它一旦建立:

// 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(); 
+0

大概,因爲你正在調用一個插件,你沒有控制在textarea創建 - $ .live是一個更好的解決方案... – Summer 2011-02-12 16:15:57

+0

海報從來沒有說過textarea是通過插件創建的,他們只是說他們使用插件來擴展其行爲。當你知道什麼時候textarea被創建並且控制它時,添加一個live()事件處理程序似乎很浪費。 – Annabelle 2011-02-16 00:00:42

4

使用jQuery 1.4,你可以這樣做:

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

jQuery的1.3.x中不支持直播()的焦點事件,所以它變得有點棘手:

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

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

die調用是有這樣的彈性只對每個textarea調用一次。

+0

但爲什麼會有人使用1.3 1.4時,就在那裏?=) – ajsie 2009-12-22 19:27:32

+0

HM我注意到,香港專業教育學院得到了1.3和1.4在他們的心不是site..where下載u能得到它? – ajsie 2009-12-22 19:28:35

+0

1.4仍然在阿爾法 - http://blog.jquery.com/2009/12/18/jquery-14-alpha-2-released/ – jimyi 2009-12-22 19:40:47

0

其他的答案表明破壞重點處理,但可能會破壞其他依賴。

相反,你可以用class =「elastic」來標記你的textarea。彈性初始化後,您可以移除「彈性」類以避免重複進入。

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