我正在使用 Jquery 解析 xml 文档并将内容添加到我的主页。
<script>
$(document).ready(function () {
$.ajax({
type: "GET",
url: "http://blog.voipinnovations.com/blog/rss.xml",
dataType: "xml",
success: function (xml) {
$(xml).find('item').each(function () {
var Col0 = $(this).find('title').text();
var Col1 = $(this).find('category').text();
var Col2 = $(this).find('author').text();
$("#RSScontainer").append("<p>" + Col0 + "</p>" + "<p>" + Col1 + "</p>" + "<p>" + Col2 + "</p> <hr />");
});
}
});
});
</script>
它工作得很好。我只有一个小问题。如何只解析最后 3 项?我尝试编写一个 for 循环,但没有成功。抱歉,如果这看起来很明显。
最佳答案
你想要这样的东西吗?使用slice jquery 的。
success: function (xml) {
$(xml).find('item').slice(-3).each(function () {
var Col0 = $(this).find('title').text();
var Col1 = $(this).find('category').text();
var Col2 = $(this).find('author').text();
$("#RSScontainer").append("<p>" + Col0 + "</p>" + "<p>" + Col1 + "</p>" + "<p>" + Col2 + "</p> <hr />");
});
}
关于javascript - jquery XML 解析最后 3 个文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35704773/