JS ajax - 在字符串中加载多个txt文件 - 顺序错误

时间:2015-06-29 16:46:18

标签: javascript ajax

我有一个加载文本文件的ajax请求。

路径如:

media/img/NAME1/descI.txt
media/img/NAME2/descI.txt

url:http://www.domain.com/media/img/"+sortTitles[i]+"/descI.txt

返回一个div中的所有txts。但我不想要那个。我想将它们放入不同的div中。例如div1中的desc1 ...

这是我的代码。 - > sortTitles[i]加载正确的文件。

    $.ajax({
  url: "http://www.domain.de/chat/media/titles.txt",
  dataType: "text",
  scriptCharset: "ISO-8859-1",
  success: function(cats) {

    sortTitles = cats.split('+++');
    Chatlength = sortTitles.length - 1;

    for (i = 2; i < Chatlength; i++) {

      catholder = document.createElement('div');
      catholder.setAttribute("id", "cat" + i + "");
      catholder.setAttribute("style", att1);

      element = document.getElementById("categories");
      element.appendChild(catholder);

      catheadwr = document.createElement('div');
      catheadwr.setAttribute("id", "headwr");
      catheadwr.setAttribute("style", att3);
      cathead = document.createElement('div');
      cathead.setAttribute("id", "head");
      cathead.setAttribute("onClick", "openshow('" + sortTitles[i] + "');");

      cath2 = document.createElement('h2');
      cath2.setAttribute("id", "h22");
      cath2.setAttribute("style", att2);

      cathimg = document.createElement('img');
      cathimg.setAttribute("id", "catbg");
      cathimg.setAttribute("src", "http://www.domain.com/media/img/" + sortTitles[i] + "/covr.jpg");

      catdesc = document.createElement('div');
      catdesc.setAttribute("id", "desc" + i + "");
      catdesc.setAttribute("style", attdesc);
      cath2text = document.createTextNode("#" + sortTitles[i] + "");

      element = document.getElementById("cat" + i + "");
      element.appendChild(catheadwr);
      catheadwr.appendChild(cathead);
      element.appendChild(catdesc);

      $.ajax({
        url: "http://www.domain.com/media/img/Mediathek/" + sortTitles[i] + "/descI.txt",
        dataType: "text",
        success: function(data) {
          i--;
          document.getElementById("desc" + i + "").innerHTML = data;

        }
      });

      cathead.appendChild(cath2);
      cathead.appendChild(cathimg);
      cath2.appendChild(cath2text);

    }
  }
});

0 个答案:

没有答案