Skip to content

Instantly share code, notes, and snippets.

@tai-sho
Last active June 16, 2017 08:00
Show Gist options
  • Save tai-sho/8f6fe9656211613dffc7 to your computer and use it in GitHub Desktop.
Save tai-sho/8f6fe9656211613dffc7 to your computer and use it in GitHub Desktop.
要改善。
;(function($) {
//最大文字数
var OVER_LENGTH = 100;
//「続きを読む」リンクのクラス
var READ_MORE_CLASS = 'read-more';
$.fn.readmore = function(option) {
if(option.length) {
OVER_LENGTH = option.length;
}
$.each(this, function() {
var text = $(this).text();
var len = text.length;
if(len > OVER_LENGTH) {
$(this).data('text',text);
$(this).text(text.substr(0, OVER_LENGTH));
$(this).append('<a href="#" class="' + READ_MORE_CLASS + '">...続きを読む</a>');
}
});
$('.' + READ_MORE_CLASS).bind('click', function() {
var parent = $(this).parent();
var origin = parent.data('text');
parent.text(origin);
return false;
});
}
});
//最大文字数
var OVER_LENGTH = 100;
//「続きを読む」リンクのクラス
var READ_MORE_CLASS = 'read-more';
/**
* .moreを指定したタグ内のテキストを省略して表示し、「続きを読む」リンクを付与します。
* @param len 最大文字数。この文字数を超えると「続きを読むリンクを作成します。」
*/
function readmore(len) {
if(len > 0) {
OVER_LENGTH = len;
}
var more = $('.more');
$.each(more, function() {
var text = $(this).text();
var len = text.length;
if(len > OVER_LENGTH) {
$(this).data('text',text);
$(this).text(text.substr(0, OVER_LENGTH));
$(this).append('<a href="#" class="' + READ_MORE_CLASS + '">...続きを読む</a>');
}
});
$('.' + READ_MORE_CLASS).bind('click', function() {
var parent = $(this).parent();
var origin = parent.data('text');
parent.text(origin);
return false;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment