我有以下代码,效果很好:
HTML:
<div data-max-characters="120">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia ea esse accusantium nobis adipisci dignissimos? Vel esse facilis perferendis ipsa? Voluptatem earum, ratione! Consequuntur et assumenda minus, aut temporibus alias.</div>
<p data-max-characters="210">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia ea esse accusantium nobis adipisci dignissimos? Vel esse facilis perferendis ipsa? Voluptatem earum, ratione! Consequuntur et assumenda minus, aut temporibus alias.</p>
jquery的:
$("div, p").each(function() {
var textMaxChar = $(this).attr('data-max-characters');
length = $(this).text().length;
if(length > textMaxChar) {
$(this).text($(this).text().substr(0, textMaxChar) + '...');
}
});
但我想用有限的话来说同样的话。有人可以帮我这个吗?
感谢。
答案 0 :(得分:3)
答案 1 :(得分:0)
你可以试试这个,
$("div, p").each(function(index, currentText) {
var length=$(this).attr("data-max-characters");
var texts=$(this).text().split(" ",length).join(" ");
$(this).text(texts);
});