用图像替换显示更多/更少+/-

时间:2013-08-29 20:42:48

标签: jquery

我有更多/更少的节目以下代码,但是我希望将文本' - ''''替换为var moreText和lessText到图形版本的一部分。任何帮助将不胜感激。

Jquery的:

<script type="text/javascript">
$(function(){

// The height of the content block when it's not expanded
var adjustheight = 85;
// The "more" link text
var moreText = "+ Read More"; 
// The "less" link text
var lessText = "- Read 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"></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);     
});

});

</script>

CSS:

.more-block p {
    font-family:Tahoma, Geneva, sans-serif; 
    font-size:14px; 
    line-height:22px;
    margin-bottom:20px;
    margin-top:0px;
}



a.adjust{

display:block;
font-weight:bold;
color:#333;
text-align:left;
text-decoration:none;
margin-top:20px;
padding-top: 10px;
padding-right: 10px;
padding-bottom: 10px;
width: 150px;
}


.more-less {
width: 300px;
}

HTML:

<div class="more-less">

    <div class="more-block">
      <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.</p>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

看起来你正在使用JQuery ......

制作本:

var moreText = "<img src='plus.gif'/> Read More"; 
// The "less" link text
var lessText = "<img src='minus.gif'/> Read Less";

然后这样做:

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

$(".adjust").toggle(function() {
    $(this).parents("div:first").find(".more-block").css('height', 'auto').css('overflow', 'visible')

        $(this).html(lessText);
}, function() {
    $(this).parents("div:first").find(".more-block").css('height', adjustheight).css('overflow', 'hidden')
    ;

    $(this).html(moreText);     
});