我有这样的元素:
"<span class=interests><a href=>Acoustics</a><a href=>Actuarial Studies</a><a href=>Administrative Law</a></span>" 我想通过a的文本截断它,例如:上面例子中的Acoustics。如果它大于50,我想截断它。
我怎样才能用jQuery做到这一点?
发布于 2014-10-04 01:49:11
尝尝这个
<span id="mainText" >
<a>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</a></span>
<script>
jQuery(document).ready(function(){
$('span a').each(function(a, v){
if ($(this).text().length>50){
$(this).remove();
}
})
})
</script>发布于 2014-10-04 02:42:27
$('.interests a').each(function(){
var truncated = $(this).text().substr(0, 50);
//Updating with ellipsis if the string was truncated
$(this).text(truncated+(truncated.length<50?'':'[...]'));
});demo
发布于 2014-10-04 01:47:13
尝试以下操作:
$('span a').each(function(){
if($(this).text().length > 50)
$(this).text($(this).text().substr(0,50));
});https://stackoverflow.com/questions/26183979
复制相似问题