展会信息港展会大全

css省略号实现text-overflow:ellipsis;
来源:互联网   发布日期:2016-03-25 15:51:59   浏览:1197次  

导读:在页面中如果我看到标题过长我们可以使用text-overflow:ellipsis;来实现省略号了,但是有时会发现不兼容,那我们也可以使用js来实现,下面我总结了一些方法。firefox7....

在页面中如果我看到标题过长我们可以使用text-overflow:ellipsis;来实现省略号了,但是有时会发现不兼容,那我们也可以使用js来实现,下面我总结了一些方法。

firefox7.0开始兼容text-overflow:ellipsis;这样的话,以后的省略号就可以做到浏览器兼容了,代码片段为:

代码如下

复制代码

width:200px;/*设置宽度*/

white-space:nowrap;/*设置不折行*/

text-overflow:ellipsis;/*这就是省略号喽*/

-o-text-overflow:ellipsis;/*兼容opera*/

overflow: hidden;/*设置超过的隐藏*/

例1.

代码如下

复制代码

<!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=utf-8" />

<title>JS test</title>

<style type="text/css">

*{

margin:0;

padding:0;

}

body{

padding:10px;

font-family:Arial;

}

#ididid{

position:relative;

width:150px;

height:20px;

line-height:20px;

text-overflow:ellipsis;

white-space:normal;

*white-space:nowrap;

overflow:hidden;

border:1px solid #999;

}

#ididid span{

position:absolute;

top:0;

right:0;

display:block;

float:left;

}

#ididid span:after{content:"...";}

</style>

</head>

<body>

<div id="ididid">woaicss woaicss woaicsswoaicsswoaicsswoaicss<span></span></div>

</body>

</html>

例.2

代码如下

复制代码

<!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=utf-8" />

<title>JS test</title>

<style type="text/css">

*{

margin:0;

padding:0;

}

body{

padding:10px;

font-family:Arial;

}

#ididid{

width:150px;

line-height:20px;

overflow:hidden;

border:1px solid #999;

}

</style>

</head>

<body>

<script type="text/javascript">

function testAuto(thisId,needLeng){

var ididid = document.getElementById(thisId);

var nowLeng = ididid.innerHTML.length;

if(nowLeng > needLeng){

var nowWord = ididid.innerHTML.substr(0,needLeng)+'...';

ididid.innerHTML = nowWord;

}

}

</script>

<div id="ididid">2323232woaicsswoaicsswoaicsswoaicss</div>

<script type="text/javascript">

testAuto('ididid',15)

</script>

</body>

</html>

赞助本站

人工智能实验室

相关热词: 开发 编程 android

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港