我目前通過一個.click事件添加一個輸入,然後想要監聽此輸入上發生的任何按鍵。但是,所插入的內容(即模糊,按鍵,焦點)不會觸發任何事件。有沒有人有什麼建議?提前致謝!Jquery .keypress動態添加的輸入
$("#recipientsDiv").click(function(){
$(this).append('< input type="text" id="toInput" class="inlineBlockElement" style="border:0px none #ffffff; padding:0px; width:20px; overflow:hidden;" />')
$("#toInput").focus();
});
$("input").keypress(function(e){
var inputStr = $(this).html();
$("#inputCopier").text(inputStr);
var newWidth = $("#inputCopier").innerWidth;
$(this).css("width", newWidth);
});
$("#toInput").blur(function(){
$("#toInput").remove();
});
我也嘗試了.keyup .keydown以及他們不工作。
我可以在複雜的情況下做些什麼,例如當我們想動態添加多個元素時?看來更好的方法是使用委託事件處理,如On()。 – QMaster 2014-02-07 13:04:03