开发者

how to hide over x chars in a string

it's very common to substring an item description if it's too long,

But开发者_C百科 how could show it back?

i was thinking

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

and then you can simply use a technique like: toogle parent's element with parent

Do you see any alternative for this?


You can do it without JavaScript assuming you don't care about support for old browsers that don't understand the CSS :hover pseudo-class (e.g., IE6 only does :hover for <a> elements).

Something like this:

<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>

In case it's not obvious what this is doing, the first CSS selector hides all elements with the class "full". The second selector shows elements with the class of "full" if they are a child of an element with class "shortened" that also has the mouse over it.

If there are other elements after the outside span they'll temporarily move over to make room while the extra text is visible - a simple way to avoid this is as follows (tweak to suite your taste):

.shortened { position: relative; }
.full { display: none; position: absolute;}
.shortened:hover .full { display: inline; z-index: 100; background-color: white;}


Well you could have a php script file that returns the full text and then use ajax to fetch the rest of the description and replace the shortened text.


Pure CSS in modern browsers: text-overflow: ellipsis; will cut the text and show in the end of box. You must specify width / height, so the text would overflow rather than make the container bigger. (Supported in IE7+, FF4+, modern Webkits and Opera)

Edit: noticed, you need exact per character cut. Then, to use my suggestion,e you will need mono-space font (and, possibly, em specified box width).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜