日期:2014-05-16  浏览次数:20319 次

js超出部分省略号

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>js超出部分省略号</title>

</head>
<body>

<ol class="list">
<li><a href="" title="0">aaaaaaaaaaa</a></li>
<li><a href="" title="1">神</a></li>
<li><a href="" title="2">神马</a></li>
<li><a href="" title="3">神马都</a></li>
<li><a href="" title="4">神马都是</a></li>
<li><a href="" title="5">神马都是浮</a></li>
<li><a href="" title="6">神马都是浮云</a></li>
<li>神马都是浮云</li>
</ol>


</body>
<script type="text/javascript">
/* 字符串截取 */
(function substr(num) {
	var obj,str,len;
	obj = document.getElementsByTagName("a");
	for (i = 0; j = obj.length, i < j; i++) {
		str = obj[i].innerHTML;
		len = str.replace(/[^\x00-xff]/g,'--').length;
		if(len > num){
			if(str.charCodeAt(0)>299){
				str = str.substr(0,num/2)+"...";
			}else{
				str = str.substr(0,num)+"...";
			}
		}
		obj[i].innerHTML = str;
	}
})(10)
</script>
</html>
?