show more/Less text with just HTML and JavaScript

前端 未结 8 2134
心在旅途
心在旅途 2020-12-10 04:45

I am needing to create a show more/less text function, but with just JavaScript and HTML.. I can\'t use any additional libraries such as jQuery and it can\'t be done with CS

8条回答
  •  醉话见心
    2020-12-10 05:21

    Hope this Code you are looking for HTML:

                

    #@item.Title

    Your Text Your Text Your Text Your Text Your Text Your Text Your Text Your Text Your Text Your Text Your Text Your Text Your Text Your Text Your Text Your Text Your Text

    SCRIPT:

        var showChar = 100;
        var ellipsestext = "[...]";
        $('.showmore').each(function () {
            $(this).find('.shorten_txt p').addClass('more_p').hide();
            $(this).find('.shorten_txt p:first').removeClass('more_p').show();
            $(this).find('.shorten_txt ul').addClass('more_p').hide();
            //you can do this above with every other element
            var teaser = $(this).find('.shorten_txt p:first').html();
            var con_length = parseInt(teaser.length);
            var c = teaser.substr(0, showChar);
            var h = teaser.substr(showChar, con_length - showChar);
            var html = '' + c + '' + ellipsestext +
            '' + h
            + '';
            if (con_length > showChar) {
                $(this).find(".shorten_txt p:first").html(html);
                $(this).find(".shorten_txt p:first span.morecontent_txt").toggle();
            }
        });
        $(".showmore").click(function () {
            if ($(this).hasClass("less")) {
                $(this).removeClass("less");
            } else {
                $(this).addClass("less");
            }
            $(this).find('.shorten_txt p:first span.moreelipses').toggle();
            $(this).find('.shorten_txt p:first span.morecontent_txt').toggle();
            $(this).find('.shorten_txt .more_p').toggle();
            return false;
        });
    

提交回复
热议问题