javascript - How to create shorten text? - Stack Overflow

I have HTML like :<div class="box"><a href="#">Stackoverflow is very us

I have HTML like :

<div class="box">
   <a href="#">Stackoverflow is very useful site and I love it</a>
</div>

and CSS like :

.box {
    width:230px;
    height:50px;
    line-height:50px;
    background:#eee;
}

.box a {
    color:#000;
}

I want create shorten text of link. If it itself overflow the box class, then take it by "...". How can I do that with css or jquery?

Check jsfiddle here

I have HTML like :

<div class="box">
   <a href="#">Stackoverflow is very useful site and I love it</a>
</div>

and CSS like :

.box {
    width:230px;
    height:50px;
    line-height:50px;
    background:#eee;
}

.box a {
    color:#000;
}

I want create shorten text of link. If it itself overflow the box class, then take it by "...". How can I do that with css or jquery?

Check jsfiddle here

Share Improve this question edited Nov 11, 2013 at 16:18 Roy M J 6,9387 gold badges53 silver badges79 bronze badges asked Nov 11, 2013 at 10:51 Hai TienHai Tien 3,1178 gold badges39 silver badges64 bronze badges 3
  • try this stackoverflow./questions/11417544/… – Vinod Louis Commented Nov 11, 2013 at 10:56
  • 1 There's no need to put it in jquery , unless if you have a button that say make it shorten – Drixson Oseña Commented Nov 11, 2013 at 10:57
  • Thanks all support me. – Hai Tien Commented Nov 11, 2013 at 11:07
Add a ment  | 

6 Answers 6

Reset to default 6

Add following styles to .box:

text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;

Working sample: http://jsfiddle/qLzK7/

You need to set text-overflow: ellipsis for that purpose see this link for more info

.box a 
{
    color:#000;
    text-overflow: ellipsis;
}

Try this:-

.box{
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}

try this,

 .box{
width:230px; 
height:50px;
line-height:50px
;background:#eee; 
overflow:hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.box a{color:#000}

http://jsfiddle/9yatw/

Try this:

$(document).ready(function() {
    var showChar = 37;
    var ellipsestext = "...";
    $('.box').each(function() {
        var content = $(this).find("a").html();
        if(content.length > showChar) {
         var a = content.substr(0, showChar);
         var b = content.substr(showChar-1, content.length - showChar);
         var html = a + ' ' + ellipsestext;
            $(this).find("a").html(html);
        }

    });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745625029a4636754.html

相关推荐

  • javascript - How to create shorten text? - Stack Overflow

    I have HTML like :<div class="box"><a href="#">Stackoverflow is very us

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信