如何使用 jQuery 将 XML 文件的内容按顺序加载到 HTML 中
如何使用 jQuery 读取名为 music.xml 的 XML 文件内容中的第一项,将其在 DIV 中显示五秒钟,读取下一个 XML 项并显示五秒钟,循环遍历 XML 文件,然后从头开始?
HTML:
<div id="music">
<div id="albumcover"></div>
<div id="songdescription"></div>
</div>
音乐.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>
How do I use jQuery to read the first item in the contents of an XML file called music.xml, display them in a DIV for five seconds, read the next XML item and display that for five seconds, loop through the XML file and then start again from the beginning?
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样的操作:
您必须调整路径(xml 文件和图像所在的位置)以及您希望将其添加到文档中的位置。目前,它将附加在结束 BODY 元素之前。
然后,我建议寻找一个轮播 jQuery 插件,它将轮流浏览它们,而不是您必须处理它的那部分。您应该查看 jCarousel Lite。
Try something like this:
You'll have to adjust the paths (for the xml file and where the images are located) as well as where you want it added in the document. Currently, it'll append right before the closing BODY element.
Then, I'd recommend looking for a carousel jQuery plugin that will rotate through them rather than you having to deal with that part of it. You should check out jCarousel Lite.
首先,我知道如何使用 jQuery 解析这些内容的方式会给
image
标签带来问题(edit: 仅当您解析文本时;XML 结果是没问题的) ),因为一旦你将它包装在 jQuery 中,它就会将text
转换为text
。你可以处理这个问题,但这并不漂亮。因此,假设您已将重命名为
。工作 jsFiddle,减去
$.ajax
请求部分。First off, the way that I know how to parse these using jQuery will give problems with the
image
tag (edit: only if you parse text; XML turns out to be fine), as once you wrap it in jQuery it will convert<image>text</image>
to<img>text
. You can deal with that, but it's not pretty. So let's assume you've renamed<image>
to<cover>
.Working jsFiddle, minus the
$.ajax
request part.