2011-06-13 66 views
0

如何使用jQuery读取名为music.xml的XML文件内容中的第一项,在DIV中显示它们五秒钟,读取下一个XML项目并显示五秒钟,循环浏览XML文件,然后从头开始重新开始?如何使用jQuery将XML文件的内容顺序加载到HTML中

HTML:

<div id="music"> 
    <div id="albumcover"></div> 
    <div id="songdescription"></div> 
</div> 

music.xml

<songs> 
    <item> 
     <image>music_01.jpg</image> 
     <description>Description of first song</description> 
    </item> 
    <item> 
     <image>music_02.jpg</image> 
     <description>Description of second song</description> 
    </item> 
    <item> 
     <image>music_03.jpg</image> 
     <description>Description of third song</description> 
    </item> 
</songs> 
+0

“music.xml”文件在哪里? – 2011-06-13 00:21:31

+0

它与HTML文件和所有JPG文件位于同一目录 – 2011-06-13 00:23:28

回答

1

首先,我知道如何使用jQuery会给与image标签问题,解析这些方式(编辑:只有当你解析文本; XML原来是罚款),因为一旦你把它包装jQuery它会将<image>text</image>转换为<img>text。你可以解决这个问题,但这不太好。因此,让我们假设您已将<image>更名为<cover>

加工jsFiddle减去$.ajax请求部分。

var music = []; 
var musicIndex = -1; 
var delay = 5000; 
var intervalId; // in case you want to clearInterval 
var $cover = $("#albumcover"); 
var $desc = $("#songdescription"); 

function rotateMusic() { 
    // Loop back to the beginning if necessary 
    if (++musicIndex === music.length) { 
    musicIndex = 0; 
    } 
    console.log(music[musicIndex].cover); 

    // Create a new image, tell it to append itself and description 
    // once it's loaded, tell it to load 
    $("<img>").load(function() { 
    $cover.empty().append($(this)); 
    $desc.text(music[musicIndex].desc); 
    }).attr("src", music[musicIndex].cover); 
} 

$.ajax({ 
    type: "GET", 
    url: "music.xml", 
    dataType: "xml", 
    success: function(xml) { 
      // Parse each item as a node 
      $(xml).find('item').each(function() { 
       // Add contents of item to music array 
       var $item = $(this); 
       music.push({ 
       cover: $item.find('image').text(), 
       desc: $item.find('description').text() 
       }); 

      }); 
      // Start rotating music, then set it to a delay 
      rotateMusic(); 
      intervalId = setInterval(rotateMusic, delay); 
      } 
}); 
+0

有一个地方的错误,我有整个JS运行在$(document).ready下,并停止了第二个图像和第三个描述的旋转。 – 2011-06-13 01:35:59

+1

我做了一些修改,以解决问题。值得注意的是,我应该把这个调用移到'$(xml)... each'声明之外的'rotateMusic'。 – brymck 2011-06-13 01:47:08

+0

太棒了,那修好了 – 2011-06-13 02:06:53

2

尝试是这样的:

$.ajax({ 
    'dataType': 'xml', 
    'success': function(xml) 
    { 
    $(xml).find('item').each(function() 
    { 
     var image = $(this).find('image').text(); 
     var description = $(this).find('description').text(); 

     $('<div id="music" />') 
     .append($('<div id="albumcover" />').append($('<img />').attr('src', image))) 
     .append($('<div id="songdescription" />').text(description)) 
     .appendTo('body'); 
    }); 

    // Add the code for the carousel here. 
    }, 
    'type': 'get', 
    'url': 'music.xml' 
}); 

你必须调整路径(用于XML文件,并在那里图像的位置)以及您希望在文档中添加的位置。目前,它会在关闭BODY元素之前追加。

然后,我建议寻找一个旋转木马jQuery插件,将通过它们旋转,而不是你必须处理它的那部分。你应该检查出jCarousel Lite

+0

如果xml文件与HTML文件和图像位于同一文件夹中,则不必从上面的脚本中调整路径。 – 2011-06-13 00:27:12

+0

就像一个注释,jQuery将解析'$(“文本”)为 '[,“text”]'。也就是说,''text“'不会是''(不是'',重要的)的孩子,而是兄弟姐妹。所以你想要替换它,即使它只是'xml.replace(/ image/i,“cover”)'。其余的很好,我想。 – brymck 2011-06-13 01:17:04

+0

@Bryan:我使用上面的代码在我的机器上测试过它,它工作正常。按照你的建议,jQuery并没有改变响应中的$(“文本”)。 – 2011-06-13 01:25:37

相关问题