【发布时间】:2015-11-25 00:52:07
【问题描述】:
我正在尝试将元素的字符通常限制为 90 个单词。但是当点击显示更多按钮时,它应该会取消限制。
我尝试使用一个类来执行此操作并将限制附加到该类,然后在单击显示更多按钮时删除该类。
我的尝试:
<header>
<h1>Heading Goes Here</h1>
<p class="jumbo-p non-exp">
The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.
</p>
<a href="#" class="more1">more</a>
</header>
CSS:
.jumbo-p{display: inline-block; height: 20px; overflow: hidden;}
.more1 {position: relative; left: 2px; top: -21px;}
.jumbo-p.expanded+.more1 {display: none}
.jumbo-p.expanded {height: auto;}
让这一切工作的 jQuery:
$(".non-exp").text(function(index, currentText) {
return currentText.substr(0, 90) + '...';
});
$(document).on('click', '.more1', function(event) {
event.preventDefault();
$('.jumbo-p').toggleClass('expanded');
$('.jumbo-p').removeClass('non-exp');
});
这对 div 设置了限制,但在删除类时不会删除该限制。请帮忙。
【问题讨论】:
-
我认为使用 text-overflow css 属性可以做到这一点。
-
text-overflow 会根据 div 的宽度来限制它。我想根据字符长度限制它
-
jsfiddle.net/z0vghqy5/1 或许能帮到你。
标签: javascript jquery html css