發(fā)表于:2011-05-16 00:00:00來(lái)源:人氣:3219
標(biāo)題太長(zhǎng)時(shí),往往會(huì)把界面撐開,影響美觀,所以我們需要省略過(guò)長(zhǎng)的文字內(nèi)容。
以下調(diào)用方法
<%= 函數(shù)名(Rs("字段"),標(biāo)題長(zhǎng)度) %>
便如:
<%= InterceptString(Rs("title"),30) %>
方法一:
Function InterceptString(txt,length)
txt=trim(txt)
x = len(txt)
y = 0
if x >= 1 then
for ii = 1 to x
if asc(mid(txt,ii,1)) < 0 or asc(mid(txt,ii,1)) >255 then
y = y + 2
else
y = y + 1
end if
if y >= length then
txt = left(trim(txt),ii)
exit for
end if
next
InterceptString = txt
else
InterceptString = ""
end if
End Function
這種方法只是截取長(zhǎng)度。不會(huì)有省略號(hào)。下面介紹一種有省略號(hào)的
方法二:
function cLeft(str,n)
dim str1,str2,alln,Islefted
str2 = ""
alln = 0
str1 = str
Islefted = false
if isnull(str) then
cleft = ""
exit function
end if
for i = 1 to len(str1)
nowstr = mid(str1,i,1)
if asc(nowstr)<0 then
alln = alln + 2
else
alln = alln + 1
end if
if (alln<=n) then
str2 = str2 & nowstr
else
Islefted = true
exit for
end if
next
if Islefted then
str2 = str2 & "..."
end if
cleft = str2
end function
=====================================================
另外在網(wǎng)上搜集到其它兩種貼出來(lái)
方法一、在ASP中使用mid()函數(shù)
<%=mid(rs("title"),1,10)%>
該代碼表示,顯示從第一個(gè)字符開始,長(zhǎng)度為10的標(biāo)題內(nèi)容
(這種方法非常簡(jiǎn)單,但多余內(nèi)容不用省略號(hào)代替)
方法二、ASP判斷語(yǔ)句
比較完美的方法
程序代碼
<%if len(rs.Fields.Item("title").Value)>10then
response.Write left(rs.Fields.Item("title").Value,10)&"......"
else
response.Write rs.Fields.Item("title").Value
end if %>