我目前正在创建一个博客站点,在主页上,我只想显示该段落的 100 个字符。之后我只想显示...(3 个点)如何使用 javascript 或 css 来实现? enter image description here
我希望它看起来像: enter image description here
最佳答案
仅使用 Html & Css,你可以使用这样的东西:
p {
width: 250px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
这里有一些例子:http://css-tricks.com/snippets/css/truncate-string-with-ellipsis/
使用 Javascript,你可以使用这样的东西:
function truncateText(selector, maxLength) {
var element = document.querySelector(selector),
truncated = element.innerText;
if (truncated.length > maxLength) {
truncated = truncated.substr(0,maxLength) + '...';
}
return truncated;
}
//You can then call the function with this
document.querySelector('p').innerText = truncateText('p', 107);
这是一个例子:http://jsfiddle.net/sgjGe/1/
关于javascript - 我怎样才能只显示帖子的 100 个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63162506/