Skip to content

Instantly share code, notes, and snippets.

@ds729
Created April 17, 2012 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ds729/2405702 to your computer and use it in GitHub Desktop.
Save ds729/2405702 to your computer and use it in GitHub Desktop.
jQueryでRSS取得(ローカル)
//http://gihyo.jp/design/serial/01/jquery-site-production/0018
jQuery(function($){
// ul要素を入れておく
var rssBlock = $('#rss').append('<ul/>');
// RSSの取得
$.ajax({
// 読み込むデータはXML
dataType: 'xml',
// 読み込みデータのURL
url: 'feed/rss2.xml',
// Ajaxの通信が成功した場合
success: function(data) {
// RSSにitem要素がひとつもなかった場合
if( $('item', data).length<1 ) {
$('ul', rssBlock).append('<li>記事がありません。</li>');
return false;
}
// item毎に・・・
$('item', data).each(function() {
// itemの中のlinkとtitleを抜き出してulに追加する
var item = $(this);
$('ul', rssBlock).append(
$('<li/>').append(
$('<a/>', {
href: $('link', item).text(),
text: $('title', item).text()
})
)
);
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment