如何只显示 post 的 100 个字符?
How can I only show 100 characters of a post?
我目前正在创建一个博客站点,在主页上,我只想要 show.After 段落的 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 Substring
<p> <%= post.body.substring(1, 100) + "..." %> </p>
<p> <%= post.content.substring(0, 100) + "..." %> </p>
这将 return 正好是 100 个字符,因为“100”表示要排除的系列中的数字。显示的字符串将从第一个元素(在索引 0 处)到第 100 个元素(在索引 99 处)。
我也在做安吉拉的挑战。哈哈!
我目前正在创建一个博客站点,在主页上,我只想要 show.After 段落的 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 Substring
<p> <%= post.body.substring(1, 100) + "..." %> </p>
<p> <%= post.content.substring(0, 100) + "..." %> </p>
这将 return 正好是 100 个字符,因为“100”表示要排除的系列中的数字。显示的字符串将从第一个元素(在索引 0 处)到第 100 个元素(在索引 99 处)。
我也在做安吉拉的挑战。哈哈!