找到你要的答案

Q:How to limit inner text of div to 175 characters?

Q:如何限制div内文字175字?

I have a div area that has an arbitrary number of characters. I want to limit the amount of characters that show up in there to 175 characters. I mostly found exaple on how to do this for a input text box or text area, but not a div. Can anyone help with this?

This is my code I am trying to limit to 175 characters:

<div class="some-area">
Blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
</div>

我有一个div区域具有任意数量的字符。我想限制在那里显示的字符数量为175个字符。我发现的例子如何输入文本框或文本区域,做到这一点,但不是一个部门可以帮助的人吗?

这是我的代码,我试图限制到175个字符:

<div class="some-area">
Blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
</div>
answer1: 回答1:

You can use the the form of text() that takes a function and write something like:

$("div.some-area").text(function(index, currentText) {
    return currentText.substr(0, 175);
});

你可以使用的形式,以()函数和写东西一样:

$("div.some-area").text(function(index, currentText) {
    return currentText.substr(0, 175);
});
answer2: 回答2:

Replace div with the correct identifier for your content:

$("div").text($(this).text().substr(0, 175)+'...');

Remove the ellipses from the end if desired. I would recommend leaving them to let your users know that the text was shortened. If you are just trying to limit the text to 175 without any notice, then you may choose to remove them in that case as well.

用正确的为您的内容标识符替换DIV:

$("div").text($(this).text().substr(0, 175)+'...');

如果需要从最终消除椭圆。我建议你留下它们让用户知道文本被缩短了。如果你只是试图限制文本到175没有任何通知,那么你可以选择删除他们在这种情况下,以及。

answer3: 回答3:

I just did this for one of my projects. I found this great plugin.

$('some-area').expander({
    slicePoint:175
});

我只是为了我的一个项目。我发现这个伟大的插件。

$('some-area').expander({
    slicePoint:175
});
answer4: 回答4:

Instead of just cutting it off at 175 characters, add an ellipsis to the div if the text runs over the maximum width: https://web.archive.org/web/20110828173959/http://plugins.jquery.com/plugin-tags/ellipsis

而不是只切割了175个字符,添加省略号的div如果文本运行在最大宽度:https://web.archive.org/web/20110828173959/http://plugins.jquery.com/plugin-tags/ellipsis

jquery