http://www.157buy.com/告诉您:网页上对于一行,溢出部分显示省略号很容易处理,直接用css就行,核心代码为overflow:hidden;text-overflow:ellipsis;
eg:
<style type="text/css">
#cs{width:100px;height:50px;line-height:50px;color:#6699ff;border:1px #ff8000 dashed;overflow:hidden;text-overflow:ellipsis}
</style>
</head>
<body>
<div id="cs">
<nobr>文字超出显示省略号,复制会复制全部哦,大家可以复制这段代码进行实验下</nobr>
</div>
但对于多行用这个方法就不行了,但也很简单,可以通过显示的字数控制,给个例子你就知道了
<script>
function init(allowedLength){
var content = document.getElementById("div1").innerHTML;
var flag = false;
while(content.replace(/[/x00-/xff]/g,"xx").length > allowedLength){
content = content.substring(0,content.length - 1);
flag = true;
}
if(flag){
content += "...";
}
document.getElementById("div1").innerHTML = content;
}
</script>
</HEAD>
<BODY οnlοad="init(20)">
<div id="div1">很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长</div>
很简单吧?如果对你有帮助,麻烦帮忙踩下“邀我去买”http://www.157buy.com/,谢谢!