如果元素描述太长,则对它进行子字符串化是很常见的,
但是怎么显示回来呢?
我在想
if string length is > x
insert after position x a <span classs="more"> //probably i'd do this through php
insert at the end of the string </span>
hide ".more" //css & javascript to hide and show
然后您可以简单地使用如下技术:toogle parent's element with parent
您看到任何替代方案吗?
最佳答案
假设您不关心对不理解 CSS 的旧浏览器的支持,您可以在没有 JavaScript 的情况下做到这一点 :hover
伪类(例如,IE6 仅对 :hover
元素执行 <a>
)。
像这样:
<style>
.full { display: none; }
.shortened:hover .full { display: inline; }
</style>
<span class="shortened">This is some text that <span class="full">has been cropped.</span></span>
如果不明显这是在做什么,第一个 CSS 选择器会隐藏所有类为“full”的元素。如果第二个选择器是类为“缩短”且鼠标悬停在其上的元素的子元素,则第二个选择器显示类为“完整”的元素。
如果在 outside span 之后还有其他元素,它们会暂时移动以腾出空间,同时可以看到额外的文本 - 一种避免这种情况的简单方法如下(根据您的口味进行调整):
.shortened { position: relative; }
.full { display: none; position: absolute;}
.shortened:hover .full { display: inline; z-index: 100; background-color: white;}
关于php - 如何隐藏字符串中的 x 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7513909/