Created
April 17, 2012 12:34
-
-
Save ds729/2405702 to your computer and use it in GitHub Desktop.
jQueryでRSS取得(ローカル)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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