【问题标题】:PHP/Javascript - Show more function for an HTML page generated by tinyMCEPHP/Javascript - 显示由 tinyMCE 生成的 HTML 页面的更多功能
【发布时间】:2018-05-26 03:28:57
【问题描述】:

我正在使用 tinyMCE,一个允许用户创建博客的文本编辑器。这可以包括页面上任何位置的文本、图像、中断等。输出有 HTML 标签。

我正在寻找一种方法来首先显示页面的一部分,并在最后显示一个“显示更多”按钮以供点击,然后再显示页面的其余内容。

我知道如何为文本内容创建一个 javascript 显示/隐藏函数,但我的问题是如何为第一部分(初始显示)和第二部分(其余内容)找到此中断的位置,因为我只能在 HTML 必须有效的地方打破它。

或者有没有办法允许整个内容,但只是通过某种类型的 CSS 在视觉上隐藏一部分内容?

一些初步的方向将不胜感激!

编辑:我会考虑使用“strtok”php 命令来获取第一段并仅显示它,但是,它似乎不起作用。举个例子:

$mystring = <<<EOF
<div>
<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 humour and the like).<img style="float: 
   left; margin: 10px;" title="DSC00286.JPG" src="images/images91/imageID1527306461424.jpg" 
   alt="" width="200" height="154" /></p><p>test test test</p>
</div>
<div>
<h2>Where does it come from?</h2>
EOF;

$partialString = strtok($mystring, '</p>');
echo $partialString; 

THE RESULT IS: div

如果有人能指出,可能会遗漏一些简单的东西?

【问题讨论】:

  • 您可以只使用隐藏溢出的 css 框高度,而不必弄乱拆分 html。然后在你的显示更多按钮上,只需从 css 中溢出,它就会自动展开到全高。

标签: javascript php html css tinymce


【解决方案1】:

将你的内容放在 div 中

HTML:

    <html>
    <head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
        <script defer src="https://use.fontawesome.com/releases/v5.0.9/js/all.js" integrity="sha384-8iPTk2s/jMVj81dnzb/iFR2sdA7u06vHJyyLlAd4snFpCl/SnyUjRrbdJsw1pGIl" crossorigin="anonymous"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
    </head>

    <body>
        <div class="blog more" style="display:none;">
            <h1>First<h1>
        </div>
        <div class="blog more" style="display:none;">
            <h1>Second<h1>
        </div>
        <div class="blog more" style="display:none;">
            <h1>Third<h1>
        </div>
        <div class="blog more" style="display:none;">
            <h1>Fourth<h1>
        </div>
        <div class="blog more" style="display:none;">
            <h1>Fifth<h1>
        </div>

        <center><div class="" id="loadMore" style="display:inline-block;">
            <a href="#" class="btn btn-success" style=" background-color:green; padding:4px 20px;" role="button"> Load More </a>
        </div></center>


     <script>
         $( document ).ready(function () {
            $(".more").slice(0, 2).show();
                if ($(".blog:hidden").length != 0) {
            $("#loadMore").show();
            }       
            $("#loadMore").on('click', function (e) {
                e.preventDefault();
                $(".more:hidden").slice(0, 2).slideDown();
            if ($(".more:hidden").length == 0) {
                $("#loadMore").fadeOut('slow');
            }
        });
     });
    </script>

    </body>
    </html> 

更改 js 中的 slice 以显示要显示或隐藏多少个 div。

【讨论】:

    【解决方案2】:

    我最终使用的方式如下:

    $minLength = 300;
    $partialArticle = preg_replace("/<img[^>]+>/", "", $fullArticle,1);
    $breakPosition = strpos($partialArticle,"</p>",$minLength);
    $partialArticle = substr($partialArticle, 0, $breakPosition+4);
    

    首先定义一个典型长度以显示为文章的初始部分。我选择了 300 个字符。接下来,我决定删除图像源,以便文章显示看起来都相似,并且只显示 4 或 5 行文字。接下来,从最小长度 300 开始,搜索下一段的结尾,这样您将获得完整的 html 结构以正确显示。使用 substr 命令将部分提取到此端点。

    【讨论】:

      【解决方案3】:

      创建 2 个 div。一个ID为initialShow,一个ID为隐藏。当页面加载时,将 id 为 initialShow 的 div 设置为 display: block;(可以在 css 中制作)。而隐藏的可以有一个显示:无;。然后,当他们单击按钮时,调用一个函数来设置 id 隐藏的 div 的样式以显示:block;

      【讨论】:

      • 好主意,但是...这些博客文件是由使用 tinyMCE 作为文本编辑器的用户创建的,他们不会进入 html 并插入特殊的 id。内容是随机的,只输出基本的 html。我的需要是找到一种简单的方法来处理这个文件并创建一个或多个逻辑块,其中html将在浏览器中正确显示,并且每次单击更多按钮时,显示剩余内容,或者显示下一个块.
      猜你喜欢
      • 2021-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-21
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多