我有一个div,其中的文本使用PHP和MySQL进行显示,其结构如下所示:
<div class="description">
<p>
Here is a lot of text.
</p>
</div>
当p标签内的文本超过100个字符时,我想显示“更多”链接。我可以使用PHP显示“阅读更多”链接,如下所示:
// strip tags to avoid breaking any html
$string = strip_tags($string);
if (strlen($string) > 100) {
// truncate string
$stringCut = substr($string, 0, 100);
// make sure it ends in a word so assassinate doesn't become ass...
$string = substr($stringCut, 0, strrpos($stringCut, ' ')).'... <a href="/this/story">Read More</a>';
}
echo $string;
问题是,当单击链接时,我想在同一DIV中显示所有文本。PHP可以做到这一点吗?还是我需要jQuery之类的东西?
如果要显示全文而不重新加载页面,则必须使用javascript / jquery。为此,全文必须在生成的HTML中。
我通过使用2 div
s来做到这一点,一个带有缩短的文本,另一个带有全文本,默认情况下是隐藏的。单击“阅读更多”时,我同时切换了div
s和将链接标签更改为“少看”。
您还可以将未缩短的文本以及省略号放在这样的元素中:
<div class="readmore">
This is the shortened text<span class="ellipsis">...</span> <span class="moreText">with the full text hidden</span> <a class="more" href="#">read more</a>
</div>
看到这个小提琴。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句