简体   繁体   English

在 html 中显示更多按钮

[英]Show more button in html

How can I make a show more button in HTML?如何在 HTML 中显示更多按钮? I'm importing some text trough a variable in how do I make it so that like 20 characters are being showed and the rest will be showed once the button is pressed.我正在通过一个变量导入一些文本,我如何制作它以便显示 20 个字符,其余的将在按下按钮后显示。 This is what I have now:这就是我现在所拥有的:

$array = explode("<br>", $array_build->content);
foreach( $array as $key => $value){
    if (
        strpos(strtolower($value),'value1') !== FALSE ||
        strpos(strtolower($value),'value2') !== FALSE ||
        strpos(strtolower($value),'value3') !== FALSE ||
        strpos(strtolower($value),'value4') !== FALSE
    ) {
        unset($array[$key]);
    }
};

$newcontent = "<pre>".implode("\n",$array)."</pre>";
?>
<div style="margin-top: 50px;"></div>
<style>
.button {
    border-radius: 4px;
    background-color: #002B56;
    color: white;
    padding: 14px 40px;
    }
</style>
<div style="text-align: center;">

<script type="text/javascript">
function toggle(obj) {
          var obj=document.getElementById(obj);
          if (obj.style.display == "block") obj.style.display = "none";
          else obj.style.display = "block";
}
</script>
<a class="button" href="javascript: void(0);" onClick="toggle('q1')">
Show More
</a>
<div style="margin-top: 50px;"></div>
<div id="q1" style="display:none;">
<div class="col" style="width: 100%;"><?= $newcontent ?></div>
</div>
</div>

Output I want:我想要的输出:

Lorem ipsum dolor sit amet. Lorem ipsum dolor 坐 amet。

SHOW MORE展示更多

consectetur adipiscing elit. consectetur adipiscing 精英。 Integer tellus erat, tempor quis dolor a, gravida eleifend leo. Integer Tellus erat,tempor quis dolor a,gravida eleifend leo。 Orci varius. Orcivarius。

As per my comment, here's a working example without any JavaScript, using <details><summary> .根据我的评论,这是一个没有任何 JavaScript 的工作示例,使用<details><summary>

How can I use this to split my text so that the preview has 20 characters or so and the summary has the rest?我如何使用它来拆分我的文本,以便预览有 20 个左右的字符,而摘要有其余的?

Note how the first few words are in the clickable <summary> element, while the rest is outside:请注意前几个单词如何在可点击的<summary>元素中,而其余的则在外面:

 details > summary { float: left; /* This makes the <summary> appear inline, as `display: inline` won't work here. */ cursor: pointer; /* Show the hand cursor. */ margin-right: 0.25em; /* Needed otherwise the space disappears, alternatively use `&nbsp;` at the end of the summary */ }
 <details> <summary>Lorem ipsum dolor sit amet</summary> consectetur adipiscing elit. Integer molestie diam sit amet scelerisque rhoncus. Sed arcu magna, fermentum nec hendrerit non, vestibulum nec enim. Nulla orci justo, hendrerit sit amet semper et, ullamcorper quis ipsum. Etiam a justo sed augue elementum accumsan non id neque. Vivamus volutpat interdum nunc non ultricies. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nullam justo lacus, sodales quis blandit non, feugiat ut lacus. Phasellus tempus sodales dui, eu auctor elit. Nam fermentum ipsum in posuere luctus. Maecenas volutpat posuere diam, ut laoreet orci iaculis et. </details>


Re: PHP回复:PHP

As you're using PHP to render the HTML, use PHP's strpos and substr to get the point in the string where it's safe to split (in this case: it looks for the first space character after character 20):当您使用 PHP 呈现 HTML 时,请使用 PHP 的strpossubstr获取字符串中可以安全拆分的点(在这种情况下:它查找字符 20 之后的第一个空格字符):

<?php

    // This PHP code is very verbose so you can understand how it works. It can be made much more shorter and succinct if necessary.

    $newcontent = "Lorem ipsum dolor...";

    $newcontentSummary   = '';
    $newcontentRemaining = '';

    $newcontentSummaryEndIdx = strpos( $newcontent, ' ', 20 );
    if( $newcontentSummaryEndIdx !== false ) {
        $newcontentSummary   = substr( $newcontent, 0, $newcontentSummaryEndIdx );
        $newcontentRemaining = substr( $newcontent, $newcontentSummaryEndIdx );
    }
?>

[...]

<?php if( $newcontentRemaining ): ?>
<details>
    <summary><?= htmlentities( $newcontentSummary ) ?></summary>
    <?= htmlentities( $newcontentRemaining ) ?>
</details>
<?php else: /* The $newcontent string is too short, so just render it directly: */ ?>
<p><?= htmlentities( $newcontent ) ?></p>
<?php endif; ?>

I think, you need to work with overflow and white-space attributes.我认为,您需要处理溢出和空白属性。

Here an examples based on your code:这是基于您的代码的示例:

 .button { border-radius: 4px; background-color: #002B56; color: white; padding: 14px 40px; } .text-container{ margin-top:20px; width:350px; display:inline-block; } .text-hidden{ white-space: nowrap; overflow: hidden; text-overflow: ellipsis; width:210px; }
 <div style="text-align: center;"> <script type="text/javascript"> function toggle(obj) { var obj=document.getElementById(obj); if (obj.classList.value.split(' ').includes("text-hidden")) obj.classList.remove('text-hidden'); else obj.classList.toggle('text-hidden'); } </script> <a class="button" href="javascript: void(0);" onClick="toggle('q1')">Show More</a> <div> <span id="q1" class="text-container text-hidden" > Lorem ipsum dolor sit amet. Consectetur adipiscing elit. Integer tellus erat, tempor quis dolor a, gravida eleifend leo. Orci varius. </span> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM