循环执行数组函数jQuery



我正在尝试创建一个简单的滑块,它可以在数组中循环并更新页面上某些元素中的文本,我有以下内容,但我不确定查看数组的最佳方式是什么?

http://jsfiddle.net/g6wvuwb3/1/

var people = [
    ['image-1.jpg', 'Thomas', 'All about thomas'],
    ['image-2.jpg', 'Jamie', 'All about Jamie'],
    ['image-3.jpg', 'Kendrick ', 'All about kendrick']
];
setInterval(function () {
    $('.quote').fadeOut(1000);
    $('.diverse-people').velocity({
        'margin-left': -$(this).width()
    }, 1000, function () {
        $(this).attr('src', people[0][0]);
        $('.diverse-people').velocity({
            'margin-left': 0
        }, 1000);
        $('.quote h6').text(people[0][1]);
        $('.quote p').text(people[0][2]);
        $('.quote').fadeIn(1000);
    });
}, 3000);

要循环遍历数组,您需要执行以下操作:

var people = [
    ['image-1.jpg', 'Thomas', 'All about thomas'],
    ['image-2.jpg', 'Jamie', 'All about Jamie'],
    ['image-3.jpg', 'Kendrick ', 'All about kendrick']
];
for(var i=0; i<people.length; i++) {
    console.log(people[i][1]);
}

不完全确定你在问什么,但希望这能回答。

编辑:

要访问数组元素,请使用以下方法:

people[0][1]

更改0和1以获得所需的密钥。

通过这种方式,您可以在数组中进行迭代。

Array.prototype.forEach.call(people, function(el){
      console.log(el[0]);
      console.log(el[1]);
      console.log(el[2]); 
});

其他方式(如果你想多次使用它。)

var forEach = Array.prototype.forEach;
forEach.call(people, function(el){
          console.log(el[0]);
          console.log(el[1]);
          console.log(el[2]); 
    });

最新更新