这应该很简单,但是开始让我烦恼。

我有以下数组:

var component_ids = [];


从我的调试器中,我知道该数组包含以下内容:

[1, 2, 3, 4]


现在要进行一些页面移位,我创建了以下功能:

    function next_page()
{
    $('#'+current_id).addClass('hidden');
    current_index++;
    current_id = component_ids[current_index]
    if(current_id != undefined && current_id != null)
    {
       $('#'+current_id).removeClass('hidden');
        if(current_id == component_ids[component_ids.length]) <-- This is the problem
        {
            $('#next').text('Færdig');
        }
    }
    if(current_index != 0)
    {
        $('#previous').removeClass('disabled');
    }
}


现在,如代码中所指出的,我想检查它是否是最后一页。这应该非常简单,但是if语句返回false或由于某种原因而被忽略。

如果我调试它并检查结果,它的确返回true,但仍然被忽略。

有没有人尝试过类似的东西?

最佳答案

由于array.length返回数组的大小(此处为4),并且数组索引从0开始,因此最后一个元素的索引为:

array.length-1


尝试:

if(current_id == component_ids[component_ids.length-1]) <-- This is the problem
        {
            $('#next').text('Færdig');
        }

关于javascript - JavaScript如果忽略句子,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25402280/

10-13 08:00
查看更多