<html>
<body>
<script language="javascript">
String.prototype.len=function(){return this.replace(/[^/x00-/xff]/g,"aa").length;}
function test()
{
alert("中国aa".len()) ;
}
</script>
<input type="text" value="" onkeypress="test();">
</body>
</html>
本文介绍了一种使用JavaScript自定义函数来计算包含特殊字符的字符串长度的方法。通过将非ASCII字符替换为固定长度的字符串,实现了对字符串真实显示长度的计算。此方法适用于需要精确控制文本显示长度的场景。
<html>
<body>
<script language="javascript">
String.prototype.len=function(){return this.replace(/[^/x00-/xff]/g,"aa").length;}
function test()
{
alert("中国aa".len()) ;
}
</script>
<input type="text" value="" onkeypress="test();">
</body>
</html>

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