0

尝试为文章创建“阅读更多/阅读更少”功能(代码见下文)。我正在使用的代码正是我需要的......但是,我无法为这个函数的更多和更少部分添加图像。当它位于“阅读更多”位置时,它应该是一个向下箭头。当处于“阅读少”位置时,它应该有向上箭头。

我怎么能做到这一点?

$(document).ready(function() {

// The height of the content block when it's not expanded
var adjustheight = 80;
// The "more" link text
var moreText = "More";
// The "less" link text
var lessText = "Less";

// Sets the .more-block div to the specified height and hides any content that overflows
$(".more-less .more-block").css('height', adjustheight).css('overflow', 'hidden');

// The section added to the bottom of the "more-less" div
$(".more-less").append('<a href="#" class="adjust is-more"></a>');

$("a.adjust").text(moreText);

$(".adjust").toggle(function() {
    $(this).parents("div:first").find(".more-block").css('height', 'auto').css('overflow', 'visible');
    $(this).text(lessText);
  }, function() {
    $(this).parents("div:first").find(".more-block").css('height', adjustheight).css('overflow', 'hidden');
    $(this).text(moreText);
});

});
4

1 回答 1

1
$("a.adjust").addClass('more-text').text(moreText);    
$(".adjust").toggle(function() {
    $(this).parents("div:first").find(".more-block").css('height', 'auto').css('overflow', 'visible');
    $(this).addClass('less-text').removeClass('more-text').text(lessText);
  }, function() {
    $(this).parents("div:first").find(".more-block").css('height', adjustheight).css('overflow', 'hidden');
    $(this).addClass('more-text').removeClass('less-text').text(moreText);
});

现在样式 css 类.more-text.less-text.

于 2012-12-05T17:55:15.883 回答