比如说一条标题实际长度是60。
我现在想在某些地方将它的长度控制在30,用LEFT或MID也可以实现,但有一个问题,这些都是把汉字做了一个字来算的,如果标题都是中文字还好的,但如果有数字或字母就不行了,会长短不一,现在这个JS是将汉字按2个来算就刚好。
<script language="javascript">
char function leftx(str,uLen)
{
var i,j,ascStr,str_tmp;
j = 0;
str_tmp = 0;
for (i = 0;i < str.length;i++)
{
str_tmp = str_tmp + str.charAt(i);
ascStr = str.charAt(i).charCodeAt(i);
if (ascStr < 0 || ascStr > 255)
j = j + 2;
else
j = j + 1;
if(j >= uLen)
{
break;
}
}
return (str_tmp);
}
</script>
用这个代码来做的话,总会出现“Microsoft VBScript 运行时错误 错误 '800a000d' 类型不匹配: 'leftx' ”
我现在想在某些地方将它的长度控制在30,用LEFT或MID也可以实现,但有一个问题,这些都是把汉字做了一个字来算的,如果标题都是中文字还好的,但如果有数字或字母就不行了,会长短不一,现在这个JS是将汉字按2个来算就刚好。
<script language="javascript">
char function leftx(str,uLen)
{
var i,j,ascStr,str_tmp;
j = 0;
str_tmp = 0;
for (i = 0;i < str.length;i++)
{
str_tmp = str_tmp + str.charAt(i);
ascStr = str.charAt(i).charCodeAt(i);
if (ascStr < 0 || ascStr > 255)
j = j + 2;
else
j = j + 1;
if(j >= uLen)
{
break;
}
}
return (str_tmp);
}
</script>
用这个代码来做的话,总会出现“Microsoft VBScript 运行时错误 错误 '800a000d' 类型不匹配: 'leftx' ”
解决方案 »
- tomcat5.0下的修改主页问题
- 请教java中String取字符的问题
- 报表导出excel表格的过程中,在展示给用户的页面上动态显示导出的状态,能做吗?
- SPRING事务的怪问题~加载就出错
- 谁有jsp购物车啊?
- spring国际化为什么会出现 No message found under code 'j_username' for locale 'zh_CN'.?
- 这个是啥子问题啊,难道我的环境配置还有问题么
- JavaEE
- 有无新版的SQLServer 2000 JDBC可以解决从数据库检索出来的中文为乱码的问题?
- 崩溃的边缘,送了100分了还是不行!!jsp虚拟目录
- 怎样写个方法来检验用户名不存在!加个鸡毛!在线等!
- 烦了很多次的问题,获取连接池后不能获取连接,找不到com.mysql.jdbc.Driver,无限郁闷!
function leftx(str,uLen)
{
var i,j,ascStr,str_tmp;
j = 0;
str_tmp = 0;
for (i = 0;i < str.length;i++)
{
str_tmp = str_tmp + str.charAt(i);
ascStr = str.charAt(i).charCodeAt(i);
if (ascStr < 0 || ascStr > 255)
j = j + 2;
else
j = j + 1;
if(j >= uLen)
{
break;
}
}
return (str_tmp);
}
</script>
function leftx(str,uLen)
{
var i,j,ascStr,str_tmp;
j = 0;
str_tmp = “”;
for (i = 0;i < str.length;i++)
{
str_tmp = str_tmp + str.charAt(i);
ascStr = str.charAt(i).charCodeAt(i);
if (ascStr < 0 || ascStr > 255)
j = j + 2;
else
j = j + 1;
if(j >= uLen)
{
break;
}
}
return (str_tmp);
}
</script>