【发布时间】:2018-10-04 05:03:07
【问题描述】:
我需要完成的很简单——将段落元素的长度限制设置为 60 个字符,之后它将显示点。我写的jQuery脚本如下:
var myDiv = $('.paragraph-24');
myDiv.text(myDiv.text().substring(0,300))
谁能帮我解决这个问题?
【问题讨论】:
我需要完成的很简单——将段落元素的长度限制设置为 60 个字符,之后它将显示点。我写的jQuery脚本如下:
var myDiv = $('.paragraph-24');
myDiv.text(myDiv.text().substring(0,300))
谁能帮我解决这个问题?
【问题讨论】:
您可以创建自定义属性,就像我为 div 标签创建了 Maxlength 一样。
如果您在 div 标签中添加 Maxlength 属性,它将在完成长度后应用点点 (...)。
<div id="address_line" Maxlength="300">
否则,它会将完整的数据附加到 div 标签中。
$(document).ready(function() {
var MaxLength = $('#address_line').attr("Maxlength");
var data = "test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test";
if (MaxLength != undefined && data.length > MaxLength) {
data = data.substring(0, MaxLength).concat("...")
}
$('#address_line').append(data);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="address_line" Maxlength="300">
</div>
【讨论】:
选项 1 - 尝试使用 <textarea>
<textarea maxlength="50">
Enter text here...
</textarea>
你可以在哪里设置maxlength
选项 2 - 使用 CSS
<div class = "paragraph-24" height="200" width="200"> abcd </div>
.paragraph-24 {
display: block;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
可以根据需要调整高度和宽度。
【讨论】: