Design & Development

jQuery 샘플 – 트윗 가져오기

걍 심심해서 jsFiddle 에 있는 예제에 몇 줄 추가해봤다.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
</head>
<body>
<p>
<input type="text" id="input"></input>
<input type="text" id="twtno" value="20"></input>
<button id="gettweet">Get Tweet</button></p>

<ol id="tweets">
</ol>
<script>
(function($){
	$.fn.tweets = function(options) {
		$.ajaxSetup({ cache: true });
		var defaults = {
			tweets: 20,
			before: "<li>",
			after: "</li>"
		};
		var options = $.extend(defaults, options);
		return this.each(function() {
			var obj = $(this);
			$.getJSON('http://search.twitter.com/search.json?callback=?&rpp='+options.tweets+'&q=from:'+options.username,
		        function(data) {
		            $.each(data.results, function(i, tweet) {
		                if(tweet.text !== undefined) {
		                    $(obj).append(options.before+tweet.text+options.after);
		                }
		            });
		        }
		    );
		});
	};
})(jQuery);

$('#gettweet').click(function(){
    $('li').replaceWith('');
    $('#tweets').tweets({
      tweets: $('#twtno').val(),
      username: $('#input').val()
    });
});
</script>  

</body>
</html>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s