rails截取字符串,点击more的时候显示

页面中的description比较长, 想截取一部分,后面的用more代替,点击more的时候显示全部。这种情况比较长见。代码如下:

在application.helper中写个方法:

def truncate_with_more (text, cutoff, id)
if text.length > cutoff
result = text[0, cutoff]
result += "<span id='text_more_link_#{id}'>…"
result += "<a href='#' οnclick='$(\"text_more_#{id}\").show(); $(\"text_more_link_#{id}\").hide(); return false;'>"
result += "more</a></span>"
result += "<span id='text_more_#{id}' style='display: none;'>"
result += text[cutoff, text.length]
result += "</span>"
else
result = text
end

result
end

页面中调用:
<%= truncate_with_more post.description, 200, "post_#{post.id}" %>

blog.csdn.net/xieqibao/archive/2007/11/02/1862796.aspx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值