jquery显示更多/更少有添加的html标签的问题

时间:2013-03-27 19:30:23

标签: jquery

我在评论div中遇到HTML标记问题,当我插入<p>something</p>标记时,没有显示更多文字链接。它与纯文本一起工作正常。我想插入富文本,但它不起作用。

HTML

<div id="header">
<H2>
    Dynamically shortened Text with Show More link using jQuery
</H2>
</div>
<p>Click here to view the Tutorial: <a href="http://viralpatel.net/blogs/2010/12/dynamically-shortened-text-show-more-link-jquery.html">Shortened Text with Show More link</a></p>

<br/>
<div class="comment more">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    Vestibulum laoreet, nunc eget laoreet sagittis,
    quam ligula sodales orci, congue imperdiet eros tortor ac lectus.
    Duis eget nisl orci. Aliquam mattis purus non mauris
    blandit id luctus felis convallis.
    Integer varius egestas vestibulum.
    Nullam a dolor arcu, ac tempor elit. Donec.</p>
</div>
<div class="comment more">
    Duis nisl nibh, egestas at fermentum at, viverra et purus.
    Maecenas lobortis odio id sapien facilisis elementum.
    Curabitur et magna justo, et gravida augue.
    Sed tristique pellentesque arcu quis tempor.
</div>
<div class="comment more">
    consectetur adipiscing elit. Proin blandit nunc sed sem dictum id feugiat quam blandit.
    Donec nec sem sed arcu interdum commodo ac ac diam. Donec consequat semper rutrum.
    Vestibulum et mauris elit. Vestibulum mauris lacus, ultricies.
</div>

CSS

body, input{
    font-family: Calibri, Arial;
    margin: 0px;
    padding: 0px;
}
a {
    color: #0254EB
}
a:visited {
    color: #0254EB
}
#header h2 {
    color: white;
    background-color: #00A1E6;
    margin:0px;
    padding: 5px;
}
.comment {
    width: 400px;
    background-color: #f0f0f0;
    margin: 10px;
}
a.morelink {
    text-decoration:none;
    outline: none;
}
.morecontent span {
    display: none;

}

的Javascript

$(document).ready(function() {
    var showChar = 100;
    var ellipsestext = "...";
    var moretext = "more";
    var lesstext = "less";
    $('.more').each(function() {
        var content = $(this).html();

        if(content.length > showChar) {

            var c = content.substr(0, showChar);
            var h = content.substr(showChar-1, content.length - showChar);

            var html = c + '<span class="moreelipses">'+ellipsestext+'</span>&nbsp;<span class="morecontent"><span>' + h + '</span>&nbsp;&nbsp;<a href="" class="morelink">'+moretext+'</a></span>';

            $(this).html(html);
        }

    });

    $(".morelink").click(function(){
        if($(this).hasClass("less")) {
            $(this).removeClass("less");
            $(this).html(moretext);
        } else {
            $(this).addClass("less");
            $(this).html(lesstext);
        }
        $(this).parent().prev().toggle();
        $(this).prev().toggle();
        return false;
    });
});

jsFiddle here

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

这是因为你正在考虑var content = $(this).html();。 html有一个<p>,当你拿到substr时,你会得到格式错误的html而没有结束标记(例如<p>Some Text)。不同的浏览器将以不同方式处理格式错误的HTML。您的morelink链接会被置于<p>之外,并且切换最终会将其隐藏起来。请改用var content = $(this).text();或找一个可以更好地处理此问题的插件。

答案 1 :(得分:0)

插件版(新功能):

$.fn.showHideParagraph = function(settings){
    var config = $.extend({},  $.fn.showHideParagraph.config, settings);
    $(this).on({click: function () {
        var $this = $(this);
        if ($this.hasClass('less')) {
            $this.removeClass('less').html(config.moreText);
        } else {
            $this.addClass('less').html(config.lessText);
        }

        $this.parent().siblings('p:not(:first)').animate({opacity: 'toggle', height: 'toggle'});
        return false;
    }
    }, '.morePLink');

    return this.each(function () {
        var $this = $(this);
        if($this.hasClass("shortenedP")) return;

        $this.addClass("shortenedP");
        var paragraphs = $this.children('p');
        if (paragraphs.length > config.paragraphShowed) {
            paragraphs.not(":first").hide();

            paragraphs.first().append(" ...");
            var moreLess = '<span><a href="#" class="morePLink">'  + config.moreText + '</a></span>';
            $this.append(moreLess);
        }
    });
};

$.fn.showHideParagraph.config = {
    paragraphShowed: 1,
    ellipsesText: "...",
    moreText: 'more',
    lessText: 'less'
};

这支持p,你可以配置p显示的数量。

小提琴:http://jsfiddle.net/544HN/