参照:Quick and Easy jQuery Read More Script Tutorial
jQueryでXMLをパースするプラグイン「jParse」を使用して、当サイトのRSSフィードから最新5件を表示し、description部分を省略表示しています。「もっと見る」をクリックすると残り部分が展開します。
<!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="ja" lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="ja" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="imagetoolbar" content="no" />
<title>設置サンプル</title>
<link rel="stylesheet" type="text/css" href="/content/lib/global.css" />
<!-- JS -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="/content/lib/jquery/jparse-0.3.3.js"></script>
<script type="text/javascript" src="/content/lib/jquery/jquery.readmore.js"></script>
<script type="text/javascript">
function start(){
$('#jparse-meta').html('<img src="/content/img/loading.gif" />');
}
function finish(){
$('#jparse-meta').remove();
/* Quick and Easy jQuery Read More Script Tutorial ここから */
// Grab all the excerpt class
$('dl#expander dd').each(function () {
// Run formatWord function and specify the length of words display to viewer
$(this).html(formatWords($(this).html(), 90));
// Hide the extra words
$(this).children('span').hide();
// Apply click event to read more link
}).click(function () {
// Grab the hidden span and anchor
var more_text = $(this).children('span.more_text');
var more_link = $(this).children('a.more_link');
// Toggle visibility using hasClass
// I know you can use is(':visible') but it doesn't work in IE8 somehow...
if (more_text.hasClass('hide')) {
more_text.show();
more_link.html(' « hide');
more_text.removeClass('hide');
} else {
more_text.hide();
more_link.html(' » more');
more_text.addClass('hide');
}
return false;
});
/* ここまで */
}
/* Quick and Easy jQuery Read More Script Tutorial ここから */
// Accept a paragraph and return a formatted paragraph with additional html tags
function formatWords(sentence, show) {
// split all the words and store it in an array
//var words = sentence.split(' ');
var words = sentence.split(''); /* 日本語対応のため''に */
var new_sentence = '';
// loop through each word
for (i = 0; i < words.length; i++) {
// process words that will visible to viewer
if (i <= show) {
//new_sentence += words[i] + ' ';
new_sentence += words[i] + ''; /* 日本語対応のため''に */
// process the rest of the words
} else {
// add a span at start
if (i == (show + 1)) new_sentence += '... <span class="more_text hide">';
new_sentence += words[i] + '';
// close the span tag and add read more link in the very end
if (words[i+1] == null) new_sentence += '</span><a href="#" class="more_link"> » more</a>';
}
}
return new_sentence;
}
/* ここまで */
$(function(){
// RSS取得
$('dl#expander').jParse({
ajaxOpts: {url: 'index.xml'},
elementTag: ['title', 'link', 'description'],
output: '<dt><a href="jpet01">jpet00</a></dt><dd>jpet02</dd>',
precallback: start,
callback: finish,
limit:5
});
});
</script>
<!-- CSS -->
<style media="screen,projection" type="text/css">
dl { margin:10px 0; padding:10px; border:1px solid #ccc; }
dt { margin:0; padding:0; font-size:11px; }
dd { font-size:11px; margin:10px 0 10px 0; padding:0; }
dt a { color:navy; }
.more { cursor:pointer; }
</style>
<link rel="stylesheet" type="text/css" href="/common/css/example.css"></head>
<body id='example3' class='example'><div class="ads" style="margin:32px auto;text-align:center;"></div><h1 class='h'><a href='/'>PHP & JavaScript Room</a> :: 設置サンプル</h1>
<h3 class='h'>実行結果</h3>
<div id="wrap">
<h1>設置サンプル</h1>
<p>参照:<a href='http://www.queness.com/post/5368/quick-and-easy-jquery-read-more-script-tutorial' target='_blank'>Quick and Easy jQuery Read More Script Tutorial</a></p>
<p>jQueryでXMLをパースするプラグイン「<a href='http://jparse.kylerush.net/'>jParse</a>」を使用して、当サイトのRSSフィードから最新5件を表示し、description部分を省略表示しています。「もっと見る」をクリックすると残り部分が展開します。</p>
<!-- CODE -->
<div id="jparse-meta" style="text-align: center; margin: 15px 0 0 0;"></div>
<dl id="expander"></dl>
<!-- / CODE -->
</div>
</body>
</html>