admin管理员组

文章数量:1316833

var txt = 'Some texts, html tags & all values i can enter through text box';
txt= txt.substring(0,255)+'...';

Can i do this thing using css?

My problem is if txt contains HTML tags, after doing a sub string the closing tag gets missing and it will break.

var txt = 'Some texts, html tags & all values i can enter through text box';
txt= txt.substring(0,255)+'...';

Can i do this thing using css?

My problem is if txt contains HTML tags, after doing a sub string the closing tag gets missing and it will break.

Share Improve this question edited Jul 25, 2013 at 5:52 slash197 9,0346 gold badges43 silver badges71 bronze badges asked Jul 25, 2013 at 5:49 ReshmaReshma 5023 gold badges9 silver badges23 bronze badges 6
  • Why are u doing a substring(). What is the expected result? – Akhil Sekharan Commented Jul 25, 2013 at 5:52
  • Please explain the issue you are having. – andrewb Commented Jul 25, 2013 at 5:53
  • 2 You can use CSS -> text-overflow:ellipsis; – adeneo Commented Jul 25, 2013 at 5:53
  • Can you give an example in "jsfiddle" – Ishan Jain Commented Jul 25, 2013 at 6:10
  • 1 thnk you every one for helping – Reshma Commented Jul 26, 2013 at 7:28
 |  Show 1 more ment

3 Answers 3

Reset to default 5

But for this you should set width of element -

Try this CSS -

 overflow: hidden;
 text-overflow: ellipsis;
 white-space: nowrap;
 width: 100%;
 display: inline-block;

See in jsfiddle

CSS will only work when you have to display the txt information on screen in some elements such as <div> or <p>

In order to make text appear with ellipses, try the following CSS properties

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

If you're using jQuery, you can use it's text() function.

var txt = 'Some texts, html tags & all values i can enter through text box';
txt= txt.text().substring(0,255)+'...';

That will display all non-html elements.

本文标签: javascriptsubstring a text with html tags using css break word or somethingStack Overflow