0

我正在从数据库中提取一些文本,我希望它出现的 div 将其缩短到一定的字符量。我正在使用这个:

脚本:

$(".activity_body").text($(this).text().substr(0, 120)+'...');

HTML:

<div class="activity_body">{{$fanartist->description}} <a href="/artists/{{$fanartist->id}}">See more...</a></div>

这并没有像应有的那样缩短文本。你知道这里可能出了什么问题吗?谢谢你。

这个div类出现多次(动态渲染),和这个有关系吗?谢谢你。

4

2 回答 2

1

this的不是指$(".activity_body")。您需要执行以下一项或一项操作:

var activityBody = $(".activity_body");
activityBody.text(activityBody.text().substr(0, 120)+'...');
// or
$(".activity_body").each(function() {
    $(this).text($(this).text().substr(0, 120)+'...');
});

这是它工作的小提琴:http: //jsfiddle.net/FSxj5/

于 2013-07-10T06:04:20.633 回答
0

如果您使用的是 php,那么您可以使用 php substr函数从服务器本身缩短字符串,例如:

substr ( $string , $start ,$length )

它返回由 start 和 length 参数指定的字符串部分。

查看php.net中的函数

于 2013-07-10T09:49:22.723 回答