移动JavaScript对象并在每次点击时递增一个?

时间:2013-08-15 18:45:49

标签: javascript jquery

我试图在点击一个div时移动到下个月我将月份商店作为对象在months = [];但是当我使用++它只是重复同一个月为什么?继承我的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的取消,那么它实际上每个广告都是1,但它只返回不是名字的数字......

5 个答案:

答案 0 :(得分:5)

每次点击都会更新myMonthName,如下所示:

$('#addone').on('click', function () {    
    countemonth = ((countemonth+1) % months.length);
    myMonthName = months[countemonth];
    $('#test span').replaceWith('<span>' + myMonthName + '</span>')
});

答案 1 :(得分: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 Ready Solution

答案 2 :(得分:2)

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

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

答案 3 :(得分:1)

执行此操作时:

var myMonthName = months[countemonth];

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

countemonth++;
myMonthName = months[countemonth];

答案 4 :(得分: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);
});

请注意,最后一个建议可以使您的浏览器无法两次查询相同的元素!如果您喜欢我在上面的示例中显示,当您需要多次使用相同的元素时,将引用存储在变量中并在任何地方使用都可能会加快您的网页速度!