Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jqueryatrr+;二维阵列问题_Javascript_Jquery_Multidimensional Array - Fatal编程技术网

Javascript jqueryatrr+;二维阵列问题

Javascript jqueryatrr+;二维阵列问题,javascript,jquery,multidimensional-array,Javascript,Jquery,Multidimensional Array,我有下面的脚本,无法找到如何在该$('#img2')中使用car[0]['img']中的值。attr('src',car[0]['img']); 我是个疯子,请解释一下。。为什么jquery不接受2d数组作为字符串值并运行函数,我的问题的可能解决方案是什么 var id = 0 ; var car = new Array(); car[0]['img'] = "./images/carousel_img_2.jpg"; ca

我有下面的脚本,无法找到如何在该$('#img2')中使用car[0]['img']中的值。attr('src',car[0]['img']); 我是个疯子,请解释一下。。为什么jquery不接受2d数组作为字符串值并运行函数,我的问题的可能解决方案是什么

var id = 0 ;
            var car = new Array();
            car[0]['img'] = "./images/carousel_img_2.jpg";
            car[0]['title'] ="title";
            car[0]['desc'] = 'longer description goes here';
            car[0]['link'] = "http://goeshere.com";
            car[1]['img'] = "./images/carousel_img_3.jpg";
            car[1]['title'] ='title';
            car[1]['desc'] = 'longer description goes here';
            car[1]['link'] = "http://goeshere.com";
            car[2]['img'] = "./images/carousel_img_2.jpg";
            car[2]['title'] ='title';
            car[2]['desc'] = 'longer description goes here';
            car[2]['link'] = "http://goeshere.com";


            function nxt () {   
        $('#img2').fadeOut('slow', function(){

            var img = car[i]['img'] ;
            $('#img2').attr('src',img);
        });
        $('#img2').fadeIn('slow');

            }

首先,去掉
img
url前面的./。我怀疑这是你目前麻烦的根源,但它不会帮助你以后摆脱困境。只需删除正斜杠并使用“
images/blah/blah.png

接下来,在哪里为该
nxt()
函数定义了
i
?现在我看到
id
在顶部设置为0,但是没有声明
I
。即使你想让它
id
我也不确定它是否有效,因为你没有在函数中迭代它

我相信你想要的是:

 function nxt (i) {   
    $('#img2').fadeOut('slow', function(){
            var img = car[i]['img'] ;
            $('#img2').attr('src',img);
    });
    $('#img2').fadeIn('slow');

 }

JavaScript对数组和字典(一个用于键/值存储的奇特术语,如关联数组)有单独的数据类型。数组要么通过Array()构造函数(如您所做的)定义,要么通过方括号[]定义,而字典则用大括号{}定义

阵列的一个示例:

var array = ['one', 'two', 'three];
alert ( array[0] ); // "one";
辞典的一个例子:

var dict = {
    'one': 'one one',
    'two': 'two two',
    'three': 'three three'
}
alert( dict.one ); // "one one"
尝试稍微修改一下数组定义:

var car = [
    {
        'img': './images/carousel_img_2.jpg',
        'title': 'title',
        'desc': 'longer description goes here',
        'link': 'http://goeshere.com'
    },
    {
        'img': './images/carousel_img_3.jpg',
        'title': 'title',
        'desc': 'longer description goes here',
        'link': 'http://goeshere.com'
    },
    {
        'img': './images/carousel_img_2.jpg',
        'title': 'title',
        'desc': 'longer description goes here',
        'link': 'http://goeshere.com'
    }
];
alert( car[0].img ); // "./images/carousel_img_2.jpg" 

也称为JavaScript对象表示法:)在我更改了数组的定义后,它工作得很好:)谢谢!!很高兴我能帮忙!作为将来的参考,使用Firebug的各种调试技术进行Javascript调试要容易得多:在本例中,一个简单的console.log(car)可能会发现数组的定义不正确。我在脚本开始时声明,但仍然不起作用。。即使在我第一次移除./它还能工作吗?在声明该变量后,尝试添加
alert(img)
。查看它发出警报的次数以及每个警报的值。正如我上面所说的,基于代码,您似乎没有正确地进行迭代。