首页 > 解决方案 > 在 ejs 的帮助下从数据库中呈现 html 时,功能无法正常工作

问题描述

我遇到了这个问题,我从数据库中渲染一些 HTML 内容,我必须添加一个阅读更多按钮,但在普通字符串的情况下,阅读更多工作正常,但对于从 DB 渲染的 HTML 内容,它是以反向模式运行,例如:点击阅读更多是做阅读更少的工作,点击阅读更少是做阅读更多的工作。我不明白问题出在哪里,或者我错过了什么。帮助将不胜感激。

问题演示:https ://playcode.io/632632/

提前致谢。

<div class="hero-content">
    <div>
        <span class="more">
            <%-showPost[0].postDescription%> 
        </span>
    </div>
</div>

<%-showPost[0].postDescription%> 正在从数据库中呈现以下 html 代码。

<h2>What is Lorem Ipsum?</h2>
<p><strong>Lorem Ipsum</strong>&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

<h2>Why do we use it?</h2>
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humor and the like).</p>
                                           

阅读更多样式和脚本如下:

<style>
    .morecontent span {
        display: none;
    }
    .morelink {
        display: block;
    }
</style>
<script>
    $(document).ready(function() {
    
    // Configure/customize these variables.
    var showChar=800;// How many characters are shown by default
    var ellipsestext = "...";
    var moretext = "Read More";
    var lesstext = "Read Less";


    $('.more').each(function() {
        var content = $(this).html();
        console.log('content=======',content)
        if(content.length > showChar) {
            console.log('====true===')
            var c = content.substr(0, showChar);
            var h = content.substr(showChar, content.length - showChar);
            console.log('====C===',c,'====H===',h)
            var html = c + '<span class="moreellipses">' + ellipsestext+ '&nbsp;</span><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().slideToggle( "slow" );
        $(this).prev().slideToggle( "slow" );
        return false;
    });
});
</script>

标签: javascripthtmljqueryejs

解决方案


推荐阅读