您的位置:首页 > 其它

div显示字符超过固定长度是显示……的方法

2016-09-18 14:07 435 查看
目前线网页遇到div显示标题的时候,如果标题过长,显示全部标题会显得很突兀,而且默认换行为首行满后再换行,有可能第二行就一个字,会很难看,这是通过CSS方法处理如下:<div style="width:754px; text-overflow:ellipsis;white-space:nowrap; overflow:hidden;">
看人家的这样写:
<pre name="code" class="html"><div style="width:100px; white-space:nowrap;overflow:hidden;text-overflow:ellipsis; border:1px solid #000;
-moz-binding: url('ellipsis.xml#ellipsis');">
说存在游览器兼容问题……???
用js不存在兼容问题:
<pre name="code" class="html">function ajustHeight(node, rows){
var v = node.innerHTML;
node.innerHTML = "";
var h1 = node.offsetHeight;
node.innerHTML = " ";
var h2 = node.offsetHeight;
var rowHeight = h2 - h1;
node.innerHTML = v;
var len = v.length, i = 3;
while(node.offsetHeight > rowHeight * rows + h1){
node.innerHTML = v.substring(0, len - i) + '...';
i++;
}
}

ajustHeight(document.getElementById('div1'), 2);
引用:http://bbs.csdn.net/topics/340175777
http://www.php100.com/html/webkaifa/DIV_CSS/2009/0416/2453.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐