2016-07-27 58 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>