2011-03-27 22 views
0

我目前正在開發一個web應用程序,並使用jGFeed從我的博客中提取RSS提要。 (最終,我會用jQTouch來構建用戶界面。)我能夠抓取我需要的內容,現在我只需要執行下一步。使用jQuery創建/顯示數組結果

我需要做的是首先顯示一個鏈接列表。這些鏈接是每個條目的標題。當用戶點擊此鏈接時,會顯示該特定條目的內容供用戶閱讀。

我沒有太多的經驗做這件事,所以我很自豪能夠得到這一點。但是現在我需要將它提升到一個新的水平,並且可以使用一些指導來處理數組,並在jQuery中使用它。任何建議或例子將不勝感激。

這裏是我的代碼:

<html> 
<head> 
<title>TEST APP</title> 
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.1.min.js"></script> 
<script type="text/javascript" src="jquery.jgfeed-min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 

    $.jGFeed('http://feeds.feedburner.com/solidverbal/clickchorus/app', function(feeds){ 
     // Check for errors 
     if(!feeds){ 
     // there was an error 
     return false; 
     } 
     // do whatever you want with feeds here 
     var title = ""; 
     var content = ""; 
     for(var i=0; i<feeds.entries.length; i++){ 
      var entry = feeds.entries[i]; 
      // Entry title 
      title = title + "<li id=\"title\"" + i + ">" + entry.title + "</li>"; 
      // Entry content 
      content = content + "<li id=\"content\"" + i + ">" + entry.content + "</li>"; 
     } 
     $("#results_titles").html("<ul>" + title + "</ul>"); 
     $("#results_content").html("<ul>" + content + "</ul>"); 

    }, 10); 
}); 
</script> 

</head> 
<body> 

<div id="results_titles">LOADING</div> 
<div id="results_content">LOADING</div> 
</body> 
</html> 
+0

我不確定我明白你需要幫助什麼問題,你能更具體地瞭解接下來需要發生什麼,或者需要修復嗎? – 2011-03-27 22:36:48

回答

0

從我個人理解,你想在股票,即使它們已經是一個數組的數組的feeds.entries。 feeds.entries是你想要的數組。因此,feeds.entries [0] .title是您需要的標題,並feeds.entries [0] .content內容。試着做這樣的事情:

//Create the <ul><li><a></a></li></ul> structure links 
var sAnchors='<ul>'; 
for(var i=0; i<feeds.entries.length; i++){ 
    var sAnchors += "<li><a href='#link' id='title_"+i+"' class='giveFeeds'>feeds.entries[i].title</a></li>"; 
} 
sAnchors += '</ul>'; 

//Append the <a>. 
$('#results_titles').append(sAnchors); 

//When the user clicks on the giveFeeds link.... 
$('.giveFeeds').live('click', function(e) { 
//Get the feed number 
var tmpId = $(e.target).attr('id'); 
//Returns the value after the '_' char and make sure it returns de number. 
var id = parseInt(tmpId.split('_')[1]); 
//Use the id value to get the desired feed 
var wantedFeed = feeds.entries[id].content; 
//Then do whatever you want with that entry 
... 
}); 

你可能會得到一些問題,瓦爾如feeds.entries,因爲你希望它是全球性的。我建議你在「for」循環中追加所有的feed(sDivs ='「+ feeds.entries [i] .content +」「;)。像這樣,你只需要使用id var(var id =(parseInt(...))知道你想要顯示的內容($('#content'+ id).show())最後,你可能想選擇添加一些動畫來使它更有趣(slideUp,slideDown,動畫)通過網絡看這些方法

+0

順便說一句,這是沒有測試過,我假設你可以調試我可以做的任何錯誤 – Bene 2011-03-28 01:39:32

+0

謝謝!我會給你一個鏡頭 – ndisdabest 2011-03-28 03:49:03

+0

我玩過這個......它絕對完美!非常感謝。 – ndisdabest 2011-03-28 13:25:21