2016-07-27 45 views
0

我想下面的jQuery后添加此HTML标记<a href="#">Read More</a>用substr JQuery返回一个html标签?

$("span.fine-print").text(function(index, currentText) { 
     if (currentText.length > 200) { 
     return currentText.substr(0, 200)+'...'; 

    } 
}); 

如何补充说,“更多”后,此链接。 任何帮助非常appriciate。谢谢,

+0

http://viralpatel.net/blogs/dynamically-shortened-text-show-more-link-jquery/ –

回答

1

您将不得不使用html()方法来获取和插入HTML,text()方法只适用于...文本。

$("span.fine-print").html(function(index, currentHTML) { 
    if (currentHTML.length > 200) { 
     return currentHTML.substr(0, 200) + '...<a href="#">Read More</a>'; 
    } 
}); 

FIDDLE

+0

这对我来说很有意思。非常感谢你。 – Udara

+0

不客气! – adeneo

0

试试这个:

var currentText = $(".fine-print").text(); 
 
if(currentText.length > 10) { 
 
    $("span.fine-print").html(currentText.substr(0, 10)+' <a href="#" class="readmore">Read More </a>'); 
 
    $(".readmore").click(function(){ 
 
    $("span.fine-print").html(currentText); 
 
    }) 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<span class="fine-print"> Lorem Ipsum Lorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem Ipsum </span>