代码之家  ›  专栏  ›  技术社区  ›  Nikola Sivkov

jquery atrr+2d数组问题

  •  2
  • Nikola Sivkov  · 技术社区  · 16 年前

    我有下面的脚本,我不知道如何在这个$('img2')中使用car[0]['img']中的值。attr('src',car[0]['img']); 我是一个J.S.Noob,所以请解释一下……为什么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');
    
                }
    
    2 回复  |  直到 16 年前
        1
  •  3
  •   c_harm    16 年前

    对于数组和字典,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" 
    
        2
  •  0
  •   Anthony    16 年前

    首先,把你面前的 img 网址。我怀疑这是你目前麻烦的根源,但以后不会帮你解决的。只需去掉正斜杠,然后继续“ images/blah/blah.png “。

    下一步,哪里是 i 为此定义 nxt() 功能?现在我看到了 id 在顶部设置为0,但没有 宣布。即使你想成功 身份证件 我不确定它是否有效,因为您没有在函数中迭代它。

    我相信你想要这样的东西:

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