7
我有一個live()
功能如下我的jquery:如何從.live更改我的代碼()來。對()
$("#qandatbl td.weight input").live("change", calculateTotal);
function calculateTotal()
{
var totalweight = hundred;
$("#qandatbl td.weight input").each(function (i, elm){
totalweight = totalweight - parseInt($(elm).val(), 10);
});
$("#total-weight").text(totalweight).append('%').css('font-weight', 'bold');
}
現在有些人說live()
功能正在放緩漸行漸遠,並且最好使用on()
函數。如果這是真的,那麼如何將上面的代碼更改爲on()
函數而不是live()
函數?是不是很重要我不使用live()
還是不重要?
謝謝:)最好的答案 – BruceyBandit 2012-01-08 10:40:13
如果對的話你應該接受他的回答。 :) – redmoon7777 2012-01-08 10:53:55
還請注意,理想情況下,您不會使用'$(document)'來設置您的處理程序:雖然這正是'.live()'最好使用最接近的父元素到您的實際目標只要在你調用'.on()'的時候該父元素存在的元素。 (可能是你的「#qandatbl」元素?) – nnnnnn 2012-01-08 11:23:55