javascript 使用 css 断字或其他东西将带有 html 标签的文本子串起来?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17850012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
substring a text with html tags using css break word or something?
提问by Reshma
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?
我可以使用 css 做这件事吗?
My problem is if txt
contains HTML tags, after doing a sub string the closing tag gets missing and it will break.
我的问题是,如果txt
包含 HTML 标记,则在执行子字符串后,结束标记会丢失并且会中断。
回答by Ishan Jain
But for this you should set width
of element -
但为此你应该设置width
元素 -
Try this CSS -
试试这个 CSS -
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
width: 100%;
display: inline-block;
回答by U.P
CSS will only work when you have to display the txt information on screen in some elements such as <div>
or <p>
当你要显示的一些元素,如屏幕上的TXT信息CSS只会工作<div>
或<p>
In order to make text appear with ellipses, try the following CSS properties
为了使文本显示为椭圆,请尝试以下 CSS 属性
p{
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
回答by Chad
If you're using jQuery, you can use it's text()
function.
如果您使用的是 jQuery,则可以使用它的text()
功能。
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.
这将显示所有非 html 元素。