How can I the limit inner text of a div to 175 characters?
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 examples on how to do this for an input text box or text area, bu开发者_JS百科t not a div. How can I do it?
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>
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);
});
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.
I just did this for one of my projects. I found this great plugin.
$('some-area').expander({
slicePoint:175
});
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
var textis="";textis=$(".some-area").html().substring(0, 175);$(".some-area").html(textis);
精彩评论