HTML長文本截取含有HTML代碼同樣適用的兩種方法
更新時間:2013年07月31日 18:05:21 作者:
正如標(biāo)題所言我們可以使用CSS截斷字符串:IE,FireFox,Opera ,Safari都兼容也可以使用js截取,具體實現(xiàn)如下,希望對大家有所幫助
方法一:用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行高自動適應(yīng)類容的高度</td>
<td>以下兩種方式都可以解決textarea行</td>
</tr>
</table>
</BODY> </HTML>
復(fù)制代碼 代碼如下:
.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截取
復(fù)制代碼 代碼如下:
<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行高自動適應(yīng)類容的高度</td>
<td>以下兩種方式都可以解決textarea行</td>
</tr>
</table>
</BODY> </HTML>
相關(guān)文章
JS組件系列之Bootstrap Icon圖標(biāo)選擇組件
這篇文章給大家介紹js組件系列之Bootstrap Icon圖標(biāo)選擇組件,對bootstrap icon圖標(biāo)相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-01-01jquery和css3中的選擇器nth-child使用方法和用途示例
nth-child(),是CSS3中的一個偽類選擇符,JQuery選擇器繼承了CSS的部分語法,允許通過標(biāo)簽名、屬性名、內(nèi)容對DOM元素進(jìn)行快速、準(zhǔn)確的選擇。2023-03-03