0

我正在尝试编写一个脚本,该脚本将生成 jQuery UI 将接受以制作手风琴的标记,如下所示。

<div id="accordion">
    <h3>2012</h3>
    <div>
        <ul>
        <li><a href="somepage.html?year=2012&month=10">October</a></li>
        <li><a href="somepage.html?year=2012&month=9">September</a></li>
        <li><a href="somepage.html?year=2012&month=8">August</a></li>
        <li><a href="somepage.html?year=2012&month=7">July</a></li>
        <li><a href="somepage.html?year=2012&month=6">June</a></li>
        <li><a href="somepage.html?year=2012&month=5">May</a></li>
        <li><a href="somepage.html?year=2012&month=4">April</a></li>
        <li><a href="somepage.html?year=2012&month=3">March</a></li>
        <li><a href="somepage.html?year=2012&month=2">February</a></li>
        <li><a href="somepage.html?year=2012&month=1">January</a></li>
    </ul>
    </div>
    <h3>2011</h3>
    <div>
        <ul>
        <li><a href="somepage.html?year=2011&month=12>December</a></li>
        <li><a href="somepage.html?year=2011&month=11>November</a></li> 
        <li><a href="somepage.html?year=2011&month=10">October</a></li>
        <li><a href="somepage.html?year=2011&month=9">September</a></li>
        <li><a href="somepage.html?year=2011&month=8">August</a></li>
        <li><a href="somepage.html?year=2011&month=7">July</a></li>
        <li><a href="somepage.html?year=2011&month=6">June</a></li>
        <li><a href="somepage.html?year=2011&month=5">May</a></li>
        <li><a href="somepage.html?year=2011&month=4">April</a></li>
        <li><a href="somepage.html?year=2011&month=3">March</a></li>
        <li><a href="somepage.html?year=2011&month=2">February</a></li>
        <li><a href="somepage.html?year=2011&month=1">January</a></li>
    </ul>
    </div>
</div>

它应该输出年份以及当前和过去的几个月,一直到 2007 年。为了创建这种类型的标记,我需要做什么?伪代码:

var d = new Date();
var yr = Number(d.getFullYear());
for (var i = 2006, i < yr, i++)
{
  //create a H3, div, and a ul
  var mo = d.getMonth();
  //some other for loop on the month to create the li items
  //how to handle date display names?
}

jQuery/Javascript 中的日期一直是我的致命弱点。

4

2 回答 2

1

这是你想要的吗??尝试演示:如果是,请回答演示,因此您必须了解更多信息以改进您的逻辑:) 尝试学习 jquery 如何append()适应临时变量以及 html 的模式

于 2012-10-28T04:32:34.877 回答
1

这就是我想出的。jsFiddle

var div = $("<div>").attr("id", "accordion");
month = [];
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var d = new Date();
var yr = Number(d.getFullYear());
for (i = yr; i > 2006; i--) {
    var m = $("<ul>");
    for (j = 1; j <= 12; j++) {
        m.append(
        $("<a>").attr("href", "somepage.html?year=" + i + "&month=" + j).text(month[j - 1]));
    }
    div.append(
    $("<h3>").text(i), m);
}
$(elementToAppendTo).append(div);​
于 2012-10-28T12:07:10.153 回答