日期:2014-05-17  浏览次数:20846 次

怎么样才能限制文章标题的字数的显示,超过的部分用省略号显示
各位,怎么样才能用ASP代码限制文章标题的字数的显示,超过的部分用省略号显示
我原来的代码如下:
<td   width= "310 "     class= "link "> <a   href= "detail.asp?id= <%=rs( "id ")%> "   > <img   src= "image/biao.gif "   width= "14 "   height= "14 "   /> <%=left(rs( "title "),100)%> </a> </td>

------解决方案--------------------
有两种方式
A:在数据库查询的时候
select case when len(新闻标题)> 100 then substring(新闻标题,1,100)+ '... ' else 新闻标题
from news where 省略(for sqlserver)
缺点在于不能正确的判断标题中数字、字母等单字节的字符,容易造成标题长短不一

B:在输出的时候(数据库查询时不进行判断)
将标题数据存入js数组,采用如下函数
//给String类型数据添加截取一段字符的函数
//特别的要支持判断双字节字符的功能
String.prototype.realLength = function(){
  return this.replace(/[^\x00-\xff]/g, "** ").length;
}
取得标题字符真实长度,再进行判断后输入。

以前常采用方法A,后来采用方法B,不过用在AJAX中
------解决方案--------------------
<%
function cutstr(tempstr,tempwid)
if len(tempstr)> tempwid then
cutstr=left(tempstr,tempwid)& "... "
else
cutstr=tempstr
end if
end function%>

<td width= "310 " bgcolor= "#CCCCCC " class= "link "> <a href= "detail.asp?id= <%=rs( "id ")%> " > <img src= "image/biao.gif " width= "14 " height= "14 " / <%=cutstr(rs( "title "),20)%> </a> </td>
------解决方案--------------------
<%Function GotTopic(Str,StrLen)
Dim l,t,c, i
l=len(str)
t=0
strlen=Clng(strLen)
for i=1 to l
c=Abs(Asc(Mid(str,i,1)))
if c> 255 then
t=t+2
else
t=t+1
end if
if t> =strlen then
GotTopic=left(str,i)& "...... "
exit for
else
GotTopic=str
end if
next
GotTopic = Replace(Replace(Replace(Replace(GotTopic, " ", "&nbsp; "),Chr(34), "&quot; "), "> ", "&gt; "), " < ", "&lt; ")
end function%>
在要截取的地方用
<%=GotTopic(rs( "title "),100)%>