javascript - Truncate Text in ul li anchor tag -
here html ,
<ul class="test"> <li><a href="#">python</a></li> <li><a href="#">truncate should apply here </a></li> <li><a href="#">c</a></li> <li><a href="#">cpp</a></li> <li><a href="#">java</a></li> <li><a href="#">.net</a></li> <li><a href="#">this long text in ul </a></li> <li><a href="#">this long text in ul </a></li> </ul>
i want truncate text in anchor tag , if length of text higher 6 chars.
the output should ,
python trun.. c cpp java .net this.. this..
how can using jquery ?
you can this:
$('ul.test li a').each(function() { var text = $(this).text(); if(text.length > 6) { $(this).text(text.substring(0, 4) + '..') } });
if want use pure css can use text-overflow: ellipsis
property:
ul.test li { width: 45px; display: block; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
however, cannot control number of limited characters before putting ellipsis using approach.
Comments
Post a Comment