1

我正在尝试通过单击 div 移动到下个月,我将月份存储为月份中的对象 = []; 但是当我使用 ++ 时,它只是在同一个月重复,为什么?这是我的 JS:

var newdate = new Date();
var getnewmonthword = newdate.getMonth();
var i = 0;
months = [];
months[0] = ('january');
months[1] = ('february');
months[2] = ('march');
months[3] = ('apri');
months[4] = ('may');
months[5] = ('june');
months[6] = ('july');
months[7] = ('august');
months[8] = ('september');
months[9] = ('october');
months[10] = ('november');
months[11] = ('december');
var countemonth = getnewmonthword;
var myMonthName = months[countemonth];

$('#test span').replaceWith('<span>' + myMonthName + '</span>')
$('#addone').on('click', function () {
    alert('fred');
    countemonth++;
    //months[++i];
   // $('#test span').replaceWith('<span>' + countemonth + '</span>')
    $('#test span').replaceWith('<span>' + myMonthName + '</span>')
});

如果我取消注释 countemonth replaceWith 它实际上每个广告 1 但它只返回数字而不是名称......

4

5 回答 5

5

myMonthName在每次点击时更新您的信息,如下所示:

$('#addone').on('click', function () {    
    countemonth = ((countemonth+1) % months.length);
    myMonthName = months[countemonth];
    $('#test span').replaceWith('<span>' + myMonthName + '</span>')
});
于 2013-08-15T18:48:55.290 回答
3

问题是你主要忘了放一条线

myMonthName = months[countemonth];

遵循完整的解决方案:

var newdate = new Date();
var getnewmonthword = newdate.getMonth();
var i = 0;
months = [];
months[0] = ('january');
months[1] = ('february');
months[2] = ('march');
months[3] = ('apri');
months[4] = ('may');
months[5] = ('june');
months[6] = ('july');
months[7] = ('august');
months[8] = ('september');
months[9] = ('october');
months[10] = ('november');
months[11] = ('december');
var countemonth = getnewmonthword;
var myMonthName = months[countemonth];

$('#test span').replaceWith('<span>' + myMonthName + '</span>')
$('#addone').on('click', function () {
    countemonth++;
    if (countemonth>=months.length) 
        countemonth = 0;

    myMonthName = months[countemonth];
    $('#test span').replaceWith('<span>' + myMonthName + '</span>')
});

Fiddler 就绪解决方案

于 2013-08-15T19:00:01.157 回答
2

myMonthName 在全局范围内。将其移动到您的点击处理程序中:

$('#addone').on('click', function () {
    alert('fred');
    countemonth++;
    var myMonthName = months[countemonth];        
    $('#test span').replaceWith('<span>' + myMonthName + '</span>')
});
于 2013-08-15T18:50:13.340 回答
1

当你这样做时:

var myMonthName = months[countemonth];

您可以从数组中获取当前值为的索引处的值countemonth。这不是实时连接。如果您countemonth稍后更改(就像您在点击处理程序中所做的那样),它不会自动更新myMonthName. 您需要在单击处理程序中基本上重复该分配:

countemonth++;
myMonthName = months[countemonth];
于 2013-08-15T18:51:03.377 回答
1

你的意思是每次点击都应该设置myMonthName变量?

这个句子...

var myMonthName = months[countemonth];

...发生一次!

总结:每次调用click事件处理程序时都需要设置整个变量!!

$('#addone').on('click', function () {
    alert('fred');
    countemonth++;

    // Now will work!
    var myMonthName = months[countemonth];

    $('#test span').replaceWith('<span>' + myMonthName + '</span>')
});

无论如何,我会将数组声明更改为:

var months = ["January", "February"]; // ... and the rest of months

还有,为什么要换span

$('#test span').replaceWith('' + myMonthName + '')

$("#test span").text(myMonthName); // Better!!!!!

而且,在一天结束时,您可以按如下方式存储 jQuery 对象:

var monthNameElement = $('#test span');

...然后在任何地方重用,即使在点击处理程序中:

var monthNameElement = $('#test span');

$('#addone').on('click', function () {
    alert('fred');
    countemonth++;

    // Now will work!
    var myMonthName = months[countemonth];
    monthNameElement.text(myMonthName);
});

请注意,最后一个建议可以让您的浏览器避免两次查询相同的元素!如果您确实像我在上面的示例中展示的那样,当您需要多次使用相同的元素时,将引用存储在变量中并在任何地方使用可能会大大加快您的网页速度!

于 2013-08-15T18:58:20.920 回答