简体   繁体   English

使用javascript变量作为img src

[英]Use javascript variable as img src

I am using the facebook Api to get some album names and their cover photos. 我正在使用facebook Api获取一些专辑名称及其封面照片。 From this names and photos i am trying to create a jquery Mobile page that presents them in a list. 从这个名字和照片,我试图创建一个jQuery Mobile页面,将它们显示在列表中。

Some of my javascript code looks like this : 我的一些JavaScript代码如下所示:

// Additional initialization code such as adding Event Listeners goes here
            FB.api('593959083958735/albums', function(response) {
                if(!response || response.error) {
                    // render error
                    alert("Noo!!");
                } else {
                    // render photos
                    for(i=0; i<response.data.length; i++){
                        albumName[i] = response.data[i].name;
                        albumCover[i] = response.data[i].cover_photo;
                        albumId[i] = response.data[i].id;

                        FB.api( albumCover[i], function(response) {
                            if(!response || response.error) {
                                // render error
                                alert("Noo!!");
                            } else {
                               // render photos
                               document.getElementById('coverPhoto').src = response.picture;
                            }
                        });


                        $("ul").append('<li>'+
                        '<a href="testFile.HTML" data-transition="slidedown">'+
                          '<img src= "nothing.jpg" id = "coverPhoto" />'+
                          '<h2>' + albumName[i] + '</h2>'+
                          '<p> Test Paragraph</p>'+
                        '</a>'+
                         '</li>')
                     .listview('refresh');

                    }
                }
            });

Array AlbumName[] has the names of the albums , and repsonse.picture has the cover picture of every album. Array AlbumName[]具有专辑的名称,而repsonse.picture具有每个专辑的封面。

As you can see in then i dynamically create a listView with the names and the pictures i get from the call. 如您所见,然后我动态创建一个listView,其中包含从调用中获取的名称和图片。 However THIS is the result . 但是, 就是结果。 The names of the albums are all ok , but the photos are messed up. 相册的名称都可以,但是照片弄乱了。 On the "network" tab i see that i get all the cover photos from the albums. 在“网络”标签上,我看到我从相册中获取了所有封面照片。 But it seems that i overwrite the cover photo only in the first cell of the listView. 但是似乎我只在listView的第一个单元格中覆盖了封面照片。 Why though? 为什么呢

As it was said id must be unique you should specify a class for each element instead, with jQuery it must looks like this : 据说id必须是唯一的,您应该为每个元素指定一个类,而对于jQuery,它必须看起来像这样:

$( ".coverPhoto" ).each(function( index ) {
       this.src = response.picture;
   );
});

Where coverPhoto is your class. 在哪里coverPhoto是您的课程。

You give the same ID to each <img> : 您为每个<img>赋予相同的ID:

<img src= "nothing.jpg" id = "coverPhoto" />

ID must be unique ID必须是唯一的

As stated by Dark Falcon, IDs MUST be unique. 如Dark Falcon所说,ID必须是唯一的。

Try something along these lines: 尝试以下方法:

...
'<img src="nothing.jpg" id="coverPhoto'+i+'" />'+
...

And in your loading code: 并在您的加载代码中:

(function(i) {
    FB.api( albumCover[i], function(response) {
        if(!response || response.error) {
            // render error
            alert("Noo!!");
        } else {
            // render photos
            document.getElementById('coverPhoto'+i).src = response.picture;
        }
    });
})(i);

The issue is the way you're rendering your photos. 问题在于您渲染照片的方式。 You are selecting element by ID but all the elements have the same ID. 您正在按ID选择元素,但是所有元素都具有相同的ID。 Each element should have a unique ID or else your selector won't work. 每个元素应具有唯一的ID,否则您的选择器将不起作用。

You should render the photo into your template instead of rendering the template and then replacing the src of the image. 您应该将照片渲染到模板中,而不是渲染模板,然后替换图像的src。 Maybe something like this: 也许是这样的:

// Additional initialization code such as adding Event Listeners goes here

var albums; // a place to get album info outside this function

FB.api('593959083958735/albums', function(response) {
  if(!response || response.error) {
    // render error
    alert("Noo!!");
  } else {
    // render photos
    for(i=0; i<response.data.length; i++){
      albums[albumID] = response.data[i] // for accessing the album info eslewhere

      var albumName = response.data[i].name;
      var albumCover = response.data[i].cover_photo;
      var albumID = response.data[i].id;

      FB.api( albumCover, function(response) {
        if(!response || response.error) {
          // render error
          alert("Noo!!");
        } else {
          // render photos
          $("ul").append('<li>'+
            '<a href="testFile.HTML" data-transition="slidedown">'+
            '<img src="'+response.picture+'" id="coverPhoto-'+albumID+'" />'+
            '<h2>'+albumName+'</h2>'+
            '<p>Test Paragraph</p>'+
            '</a>'+
            '</li>')
          .listview('refresh');
        }
      });
    }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM