我使用jQuery的彈性插件來擴展文本框。它工作的很好,但我想用一個文本框添加到DOM與Ajax,但不幸的是這個插件沒有實時內置函數。住jquery彈性插件?
有沒有辦法解決這個問題?
http://www.unwrongest.com/projects/elastic/
我使用jQuery的彈性插件來擴展文本框。它工作的很好,但我想用一個文本框添加到DOM與Ajax,但不幸的是這個插件沒有實時內置函數。住jquery彈性插件?
有沒有辦法解決這個問題?
http://www.unwrongest.com/projects/elastic/
如果你有超過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();
使用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調用一次。
其他的答案表明破壞重點處理,但可能會破壞其他依賴。
相反,你可以用class =「elastic」來標記你的textarea。彈性初始化後,您可以移除「彈性」類以避免重複進入。
$(document) .on('focus','textarea.elastic',function(e) { $(this).removeClass('elastic').elastic(); })
大概,因爲你正在調用一個插件,你沒有控制在textarea創建 - $ .live是一個更好的解決方案... – Summer 2011-02-12 16:15:57
海報從來沒有說過textarea是通過插件創建的,他們只是說他們使用插件來擴展其行爲。當你知道什麼時候textarea被創建並且控制它時,添加一個live()事件處理程序似乎很浪費。 – Annabelle 2011-02-16 00:00:42