2016-07-27 2 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)+'...'; 

    } 
}); 

이 후 링크 "자세히보기"라는 추가하는 방법. 도움이 있으면 도움을 받으십시오. 감사,

+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

그것은 나를 위해 woking. 고맙습니다. – 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>

관련 문제