如何在jquery脚本中循环?

时间:2013-10-19 12:06:24

标签: javascript jquery timeline

我正在编写一个脚本,它从服务器获取一些记录并将其放在时间轴上。时间轴脚本使用jQuery,raphael和qtip生成时间轴。时间轴上的点显示事件。这个点放在时间轴上。由于我有一些记录是时间轴上的点,我不能通过将年,月和日期保持为变量来循环“日期”。

记录返回日期,月份和年份以及记录名称。

这是原始代码:

$(document).ready(function(){

      var events = [
      {dates: [new Date(2012, 4, 5)], title: "some name", section: 0}
      ];

      var timeline1 = new Chronoline(document.getElementById("target1"), events,
        {animated: true,
         tooltips: true,
         defaultStartDate: new Date(2012, 3, 5),
         sections: sections,
         sectionLabelAttrs: {'fill': '#997e3d', 'font-weight': 'bold'},
          draggable: true
      });

});

所以我想到了这个:

$(document).ready(function(){

  var events = [

  for (var i = 0; i < totalrecordsretreived; i++)
  {
     var x,y,z, recname;

     recname = retrievedrecord[i].name;
     x = retrievedrecord[i].day;
     y = retrievedrecord[i].month;
     z = retrievedrecord[i].year;

    //code for putting record into x,y,z and recname

    {dates: [new Date(z, y, x)], title: recname, section: 0} // creates the dot.
  }

  ];

  var timeline1 = new Chronoline(document.getElementById("target1"), events,
    {animated: true,
     tooltips: true,
     defaultStartDate: new Date(2012, 3, 5),
     sections: sections,
     sectionLabelAttrs: {'fill': '#997e3d', 'font-weight': 'bold'},
      draggable: true
  });
});

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

哦,我明白了......

将您的代码更改为:

  var events = [];

  for (var i = 0; i < totalrecordsretreived; i++)
  {
     var x,y,z, recname;

     recname = retrievedrecord[i].name;
     x = retrievedrecord[i].day;
     y = retrievedrecord[i].month;
     z = retrievedrecord[i].year;

    //code for putting record into x,y,z and recname

    events.push({dates: [new Date(z, y, x)], title: recname, section: 0}); // creates the dot.
  }

基本上上面的代码是创建一个空的Array事件。然后利用Array类的push方法来填充它。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

如果有帮助,请告诉我。