-2
<body>

  <input type="text" id="search" onchange="doSearch()" />

  <div id="catalog">
  </div>

  <div id="each_track">
  </div>

  <script>
  SC.initialize({
    client_id: '*****************'
  });

  function doSearch() {
    var searchTerm = document.getElementById('search').value;

        // Search soundcloud for artists
        SC.get('/tracks', { q: searchTerm, license: 'cc-by-sa' }, function(tracks) {
          for(track in tracks) {
            var img = document.createElement('img');
            var catalog = document.getElementById('catalog');
            img.setAttribute("src",tracks[track]["artwork_url"]);
            catalog.appendChild(img);
            }
         }
        });
      };
      </script>
    </body>

Just append a div to each img rather than all the images to the div, in response to the comments.

4

2 回答 2

2

尝试

function doSearch() {
    var searchTerm = document.getElementById('search').value;

    // Search soundcloud for artists
    SC.get('/tracks', { q: searchTerm, license: 'cc-by-sa' }, function(tracks) {
        for(track in tracks) {
            var img = document.createElement('img');
            var catalog = document.getElementById('catalog');
            img.setAttribute("src",tracks[track]["artwork_url"]);

            var div = document.createElement('div');
            div..appendChild(img);

            catalog.appendChild(div);
        }
    });
};
于 2013-04-25T00:28:17.213 回答
1
   for(track in tracks) {
        var img = document.createElement('img');
        var catalog = document.getElementById('catalog');
        var div = document.createElement('div');


        img.setAttribute("src",(tracks[track]["artwork_url"]));
        div.setAttribute("class","music");

        if((tracks[track]["artwork_url"])){
          catalog.appendChild(div);
          div.appendChild(img);
        }

与您的原始答案类似,if 循环针对每个图像元素

于 2013-04-25T13:48:47.567 回答