<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gbk">
<meta name="generator" content="www.cxybl.com">
<title>www.cxybl.com</title>
</head>
<body>
<input type=text value="www.cxybl.com" id="test" name="test"><button >测试</button>
</body>
<script type="text/javascript">
function getbyteslength(str) {
// 在gbk编码里,除了ascii字符,其它都占两个字符宽
return str.replace(/[^\x00-\xff]/g, 'xx').length;
}
function showlen(){
var word = document.getelementbyid("test").value;
alert(getbyteslength(word));
}
</script>
</html>
本文链接http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20120708/31775.html
时间: 2025-01-18 20:46:12