HTML长文本截取含有HTML代码同样适用的两种方法
更新时间:2013年07月31日 18:05:21 作者:
正如标题所言我们可以使用CSS截断字符串:IE,FireFox,Opera ,Safari都兼容也可以使用js截取,具体实现如下,希望对大家有所帮助
方法一:用CSS截断字符串:IE,FireFox,Opera ,Safari都兼容
.subLongText{
width:150px;
height:24px;
overflow:hidden;
white-space:nowrap;
text-overflow:ellipsis;
text-overflow: ellipsis;/* IE/Safari */
-ms-text-overflow: ellipsis;
-o-text-overflow: ellipsis;/* Opera */
-moz-binding: url("ellipsis.xml#ellipsis");/*FireFox*/
}
<span class="subLongText">任意长度文本</span>
方法二:用js截取
<HTML>
<HEAD>
<TITLE>JQUERY 文本截取方法</TITLE>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript">
$.fn.substr = function(length,content){
$(this).each(function(i,item){
var val=$(item).html();
if(!val) return;
if(val.length>length) {
val = val.substring(0,length);
val += content || "..."
$(item).html(val);
}
});
}
function subTdContent(){
$('td').substr(20);
}
</script>
</HEAD>
<BODY onload="javascript:subTdContent();">
<table style="border:1px solid #b3c0f5;" border="1">
<tr>
<td>我是代码:$.fn.substr = function(l,c){ $(this).each(function(i,item)</td>
<td>以下两种方式都可以解决textarea行高自动适应类容的高度</td>
<td>以下两种方式都可以解决textarea行</td>
</tr>
</table>
</BODY> </HTML>
复制代码 代码如下:
.subLongText{
width:150px;
height:24px;
overflow:hidden;
white-space:nowrap;
text-overflow:ellipsis;
text-overflow: ellipsis;/* IE/Safari */
-ms-text-overflow: ellipsis;
-o-text-overflow: ellipsis;/* Opera */
-moz-binding: url("ellipsis.xml#ellipsis");/*FireFox*/
}
<span class="subLongText">任意长度文本</span>
方法二:用js截取
复制代码 代码如下:
<HTML>
<HEAD>
<TITLE>JQUERY 文本截取方法</TITLE>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript">
$.fn.substr = function(length,content){
$(this).each(function(i,item){
var val=$(item).html();
if(!val) return;
if(val.length>length) {
val = val.substring(0,length);
val += content || "..."
$(item).html(val);
}
});
}
function subTdContent(){
$('td').substr(20);
}
</script>
</HEAD>
<BODY onload="javascript:subTdContent();">
<table style="border:1px solid #b3c0f5;" border="1">
<tr>
<td>我是代码:$.fn.substr = function(l,c){ $(this).each(function(i,item)</td>
<td>以下两种方式都可以解决textarea行高自动适应类容的高度</td>
<td>以下两种方式都可以解决textarea行</td>
</tr>
</table>
</BODY> </HTML>
相关文章
微信小程序开发自定义tabBar实战案例(定制消息99+小红心)
一定的需求情况下无法使用小程序原生的tabbar的时候,需要自行实现一个和tabbar功能一模一样的自制组件,下面这篇文章主要给大家介绍了关于微信小程序开发自定义tabBar(定制消息99+小红心)的相关资料,需要的朋友可以参考下2022-12-12
原生JavaScript实现动态省市县三级联动下拉框菜单实例代码
像平时购物选择地址时一样,通过选择的省动态加载城市列表,通过选择的城市动态加载县区列表,从而可以实现省市县的三级联动,下面使用原生的JavaScript来实现这个功能,需要的朋友参考下吧2016-02-02
让JavaScript的Alert弹出框失效的方法禁止弹出警告框
弹出框难免会影响你的心情,所以通过以下代码可将Js弹出框屏蔽掉,实现思路是对alert方法重写2014-09-09


最新评论