【发布时间】:2011-12-06 14:26:35
【问题描述】:
我有一个 XML 文档(如下):
<?xml version="1.0" encoding="UTF-8"?>
<testimonials>
<testimonial user="User Name 1" state="1" comment="Comment 1"></testimonial>
<testimonial user="User Name 2" state="2" comment="Comment 2"></testimonial>
<testimonial user="User Name 3" state="3" comment="Comment 3"></testimonial>
<testimonial user="User Name 4" state="4" comment="Comment 4"></testimonial>
<testimonial user="User Name 5" state="5" comment="Comment 5"></testimonial>
</testimonials>
我通过一个 jQuery ajax 调用来获取这些数据。现在我想在一个框(#xmlFeed)中显示它并淡入淡出到下一个节点。它应该在移动到下一个节点之前显示几秒钟,再次淡入淡出。
到目前为止,我的代码在下面,但我无法让循环和淡入淡出正常工作。代码如下:
<!doctype html>
<html>
<head>
<title></title>
</head>
<body>
<div id="container">
<div id="xmlFeed"></div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
$(function(){
//Initial load
getXML();
});
getXML = function(){
$.ajax({
type: "GET",
url: "xml/xmlFeed.xml",
dataType: "xml",
success: function(xml){
var data = $( xml );
var findTestis = data.find("testimonial");
findTestis.each(function(i){
name = this.getAttribute("user");
state = this.getAttribute("state");
comment = this.getAttribute("comment");
contents = " <div id='listing" + i + "'>"
+ "<p><strong>" + comment + "</strong></p>"
+ "<p>" + name + ", " + state + "</p>"
+ "</div>";
setTimeout(function(){
$("#xmlFeed").html(contents);
}, 2000 * i );
});
}
});
}
</script>
</body>
目前它正在工作并进行显示,但它只是一遍又一遍地显示最后一个节点的内容。我猜我需要更新增量或类似的东西。将此视为 RSS 样式功能。
感谢您的帮助,请举例说明。
【问题讨论】:
-
伙计们,我可以请人修改我的代码,以便按照我想要的方式工作。我需要这个尽快,我感谢迄今为止的所有帮助,但它没有让我到任何地方。
-
好吧,如果我的回答对您没有任何建议,您为什么不使用自己的计数器而不是依赖 i。
标签: javascript jquery html xml ajax