最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to create shorten text? - Stack Overflow

matteradmin4PV0评论

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);
        }

    });
});
Post a comment

comment list (0)

  1. No comments so far