2016-07-27 5 views
0

私はjqueryのこの後のリンク「続きを読む」ことを追加する方法substr JQueryを使用してHTMLタグを返しますか?

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

    } 
}); 

に従った後、このhtmlタグ<a href="#">Read More</a>を追加します。 非常に助かります。ありがとう、

+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>

関連する問題