我正在尝试添加执行此代码,当代码溢出时,会在文本末尾添加省略号:
var ph=$('.txt-entries .txt-entries-wrap');
var p=$('.txt-entries .txt-entries-wrap p');
var divh=$('.txt-entries').height();
while ($(ph).outerHeight()>divh) {
$(p).text(function (index, text) {
return text.replace(/\W*\s(\S)*$/, '...');
});
}
改编自此帖子Cross browsers mult-lines text overflow with ellipsis appended within a width&height fixed div?
要几个div元素,但当我将它放在每个循环中时会导致它崩溃。这是 HTML :
<div class="txt-block">
<div class="txt-entries">
<div class="txt-entries-wrap">
<h2>Lorem ipsum</h2>
<p class="copy">Lorem ipsum</p>
</div>
</div>
<div class="cta-aligner">
<a class="cta-2" href="#">
Lorem ipsum
</a>
</div>
</div>
<div class="txt-block">
<div class="txt-entries">
<div class="txt-entries-wrap">
<h2>Lorem ipsum</h2>
<p class="copy">Lorem ipsum</p>
</div>
</div>
<div class="cta-aligner">
<a class="cta-2" href="#">
Lorem ipsum
</a>
</div>
</div>
如何为所有.txt-block
元素执行它?