jquery.append() - only the last element of my list is appended, previous ones are erased

I have a page like this :
and some javascript to fill my calendar like this function getWeek(){ $.getJSON("/getWeek",function(events){ var eventHeight = $("#hoursTable > div").height(); var eventWidth = $("#daysTable > div").width(); var startWeek = events[0]// timestamp of the start of the week for(var i = 1; i < events.length; i ++){ $(".day").empty(); var startHour = (events[i].startDate - startWeek)/3600 var duration = (events[i].stopDate - startWeek)/3600 - startHour var dayStart = Math.floor(startHour/24); var startHour = startHour - dayStart * 24 divEvent = $('
') .width(eventWidth-2) .height(duration*eventHeight) .css("border","1px solid black") .css("margin-top",startHour*eventHeight) .html(events[i].name); divEvent.appendTo("#day"+dayStart); console.log(divEvent); } }); } my problem being : events contain 3 element I'd like to display but only the last is displayed. If I stop my "for" at the first iteration I can see the first div appended, but it seems that if my loop goes for three iteration the two previous are deleted. The console.log() display some "not-anymore" existing element. Any idea ?
You may want to avoid setting styles like borders and margins directly in your javascript as these can change if the design ever changes. Instead, use addClass() to add classes to your elements and then let an external CSS file dictate the look of the calendar.

以上就是jquery.append() - only the last element of my list is appended, previous ones are erased的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准