页面中的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='#' onclick='$(\"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.youkuaiyun.com/xieqibao/archive/2007/11/02/1862796.aspx
在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='#' onclick='$(\"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.youkuaiyun.com/xieqibao/archive/2007/11/02/1862796.aspx
本文介绍了一种常见的网页展示需求:当描述过长时进行截断,并提供展开按钮来显示完整内容的方法。通过自定义辅助方法实现文本的优雅截断及交互。
1261

被折叠的 条评论
为什么被折叠?



