【发布时间】:2015-10-12 11:23:52
【问题描述】:
我目前有一个 jQuery 的 sn-p,当子字符串大于父容器时,它成功地截断了我的文本(见下文)。
var initial = $('.js-text').text();
$('.js-text').text(initial);
while($('.js-text').outerHeight() > $('.js-text-truncator').height()) {
$('.js-text').text(function(index, text) {
return text.replace(/\W*\s(\S)*$/, '...');
});
}
我已经使用了我所拥有的语义,因为我计划在现有标记中的多个位置使用这个小 jQuery 组件,如下所示:
<div class="existing-first-div-container js-text-truncator">
<p class="existing-first-string js-text">The quick brown fox</p>
</div>
<div class="existing-second-div-container js-text-truncator">
<p class="existing-second-string js-text"> jumped over the lazy red dog.</p>
</div>
如果你已经读到这里并且已经猜到了我的问题顺便说一句......
所以问题是我的 jQuery 正在存储文本,但它正在存储 all 文本。因此,这两个现有的 div 都被截断,但最终都显示为“快速的棕色狐狸跳过了懒惰的红狗”。而不是第一读“敏捷的棕狐”和第二读“跳过懒惰的红狗”。
是否可以按照我的意图使用我的 js-text-truncator 作为我的标记的扩展,而不是一次存储所有截断的文本实例?
【问题讨论】:
标签: jquery html scalability markup truncate