我想为每个DIV(.topitembox)创建倒计时,为此我必须添加那些json vars
$('#countdown_items .topitembox').each(function () {
itmID = $(this).attr('class').replace(/[^0-9]/g, '');
$(this).append('<div id="countdown' + itmID + '"></div>');
$.get(getTimeLeft, function (data) {
var Day = data.Day,
Hours = data.Hour,
Mins = data.Min,
Secs = data.Sec,
dayH = Day * 24,
Hours = Hours + dayH;
// i have more id's: countdown1, countdown2... I need to get the variable dates for each div id
$('#countdown1').countdowntimer({
hours: Hours,
minutes: Mins,
seconds: Secs,
timeUp: timeisUp
});
}, "json");
});
我在json中尝试使用$(this)并且它无效。还有另外一个功能吗?
答案 0 :(得分:1)
你需要将$.get
包装在一个闭包中。
$('#countdown_items .topitembox').each(function () {
var itmID = $(this).attr('class').replace(/[^0-9]/g, '');
$(this).append('<div id="countdown' + itmID + '"></div>');
(function (itmID) { //Notice Here
$.get(getTimeLeft, function (data) {
var Day = data.Day,
Hours = data.Hour,
Mins = data.Min,
Secs = data.Sec,
dayH = Day * 24,
Hours = Hours + dayH;
//Now itmID is accesible here
$('#countdown' + itmID).countdowntimer({ //Notice Here
hours: Hours,
minutes: Mins,
seconds: Secs,
timeUp: timeisUp
});
}, "json");
})(itmID);//Notice Here
});