开发者

Limiting string text within div

开发者 https://www.devze.com 2023-02-02 02:44 出处:网络
Can someone tell me if their is a way to limit text within div classes. example <div class=\"mon\">December</div>

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消