jQuery dotdotdotプラグインを使用して、表示するテキストが多い場合にコンテンツ全体を表示および非表示にするための [ More ] ボタンと[ Less ] ボタンの両方を使用したいと考えています。<div>
[その他] ボタンは正常に機能していますが、元の表示に戻す方法はまだわかりません<div>
。これは、dotdotdot を使用して切り詰められた文字列を拡張する方法だけではないことに注意してください。これには、長い文字列を再切り詰める Less ボタンが組み込まれているためです。
これが私のコードです:
$(function() {
$("div.ellipsis-text").dotdotdot({
after: 'a.more',
callback: dotdotdotCallback
});
$("div.ellipsis-text").find("a").click(function() {
if ($(this).text() == "More") {
var div = $(this).closest('div.ellipsis-text');
div.trigger('destroy').find('a.more').hide();
div.css('max-height', '');
$("a.less", div).show();
}
else {
$(this).text("More");
$(this).closest('div.ellipsis-text').css("max-height", "50px").dotdotdot({ after: "a", callback: dotdotdotCallback });
}
});
function dotdotdotCallback(isTruncated, originalContent) {
if (!isTruncated) {
$("a", this).remove();
}
}
});
のアンカー タグのclick
イベント ハンドラー<div>
が削除されているようです。[その他] ボタンをクリックした後、イベント ハンドラーにアクセスできません。
見つかった解決策:
$(function() {
$("div.ellipsis-text").dotdotdot({
after: 'a.more',
callback: dotdotdotCallback
});
$("div.ellipsis-text").on('click','a',function() {
if ($(this).text() == "More") {
var div = $(this).closest('div.ellipsis-text');
div.trigger('destroy').find('a.more').hide();
div.css('max-height', '');
$("a.less", div).show();
}
else {
$(this).hide();
$(this).closest('div.ellipsis-text').css("max-height", "50px").dotdotdot({ after: "a.more", callback: dotdotdotCallback });
}
});
function dotdotdotCallback(isTruncated, originalContent) {
if (!isTruncated) {
$("a", this).remove();
}
}
});
みんなありがとう!