Can someone tell me if their is a way to limit text within div classes.
example
<div class="mon">December</div>
<div class="mon">November</div>
<div class="mon">October</div>
$('.mon').hide().text();
var month = $('.mon').text().substr(0,3);
$('.mon').append(month).show();
alert(month);
trying to开发者_运维知识库 output
Dec Nov Oct
Any help?
.text()
can take a function, like this:
$('.mon').text(function(i, t) { return t.substr(0, 3); });
You can test it out here.
You can do this:
$('div.mon').text(function() {
return $(this).text().substring(0, 3);
});
Live example
text
, like a lot of other "setter" functions of jQuery (html
, attr
, etc.), accepts a function it will call to get the value to set for each element.
精彩评论