1
我想添加一個省略號的內容與換行符,如果它溢出一個div與最大高度和溢出:隱藏。如何使用max-height和overflow添加省略號到div的內容(包括換行符):hidden?
我試過這樣做,但我的方法的問題是如果有兩個換行符,只有一個會顯示在輸出中。這是本小提琴鏈接https://jsfiddle.net/AkshayaKT/qg8yurt5/
var ellipsesText = "...";
$('.card-content').each (function() {
var content = $(this).html(),
divheight = $(this).height(),
lineheight = $(this).css('line-height').replace("px", ""),
lines = Math.round(divheight/parseInt(lineheight)),
eachLines = content.split(/<br>/);
if (lines >= 3) {
var replaceTxt = "";
$.each(eachLines, function(index, item) {
if (index <= 2) {
if (item == "") {
replaceTxt = replaceTxt + '<br>';
} else {
replaceTxt = replaceTxt + item;
}
}
});
replaceTxt = replaceTxt + ellipsesText;
$(this).html(replaceTxt);
}
});
body {
box-sizing: border-box;
}
.card-content {
font-size: 15px;
font-weight: 400;
color: #333;
max-height: 63px;
margin: 10px;
line-height: 21px;
overflow: hidden;
border: 1px solid;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="card-content">What is Lorem Ipsum?<br><br><br><br>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's</div>
<div class="card-content">What is Lorem Ipsum?<br><br>hi</div>
FWIW,在OP的代碼具有不同的時候''
自己行VS擠在一行。 – hungerstar
不確定你的意思是什麼時候「
'在他們自己的行上」,但是我的修復程序現在應該具有所需的行爲(多個'
'仍然顯示,並且橢圓與最後一行添加在同一行上第3行))。運行上面的代碼片段,你會發現它工作得很好:-) –
無論代碼中如何放置'
',你的代碼都能正常工作。在你用代碼更新你的答案之前,我已經評論過了。使用OP的原始代碼,如果'
'被放置在不同的行上,就像您在示例中所做的那樣,您將得到不同的結果,[結果一](http://jsfiddle.net/AkshayaKT/qg8yurt5/)vs [結果二](http://jsfiddle.net/qg8yurt5/7/)。你的代碼不會發生這種情況。我會很快刪除我的評論。 – hungerstar