2012-03-21 4 views
1

이 코드가 있습니다.JQuery는 요소로 시작하는 텍스트를 가져옵니다.

<p>jQuery is free, open source software, dual-licensed under the <span>MIT License</span> or the GNU General Public License, Version 2.</p> 
<p><span id="elt">jQuery is a</span> cross-browser <span>JavaScript</span>library designed to simplify the client-side scripting of HTML. <span>It was released in January 2006</span> at BarCamp NYC by John Resig. Used by over 52% of the 10,000 most visited websites, jQuery is the most popular JavaScript library in use <span>today</span>.</p> 
<img src="character1.jpg" height="200"/> 
<p>jQuery is free, open source software, dual-licensed under the <span>MIT License</span> or the GNU General Public License, Version 2.</p> 
<p>... 

는 내가 처음 500 개 문자는 다음과 같이 $(".elt") 시작 싶어 :

jQuery is a cross-browser JavaScript library designed to simplify the client-side scripting of HTML.It was released in January 2006 at BarCamp NYC by John Resig. Used by over 52% of the 10,000 most visited websites, jQuery is the most popular JavaScript library in use today.jQuery is free, open source software, dual-licensed under the MIT License or the GNU General Public License, Version 2. 

단지 텍스트, 모든 html 태그를 제거합니다.

답변

4
$('#elt').parent().text().slice(0, 500); 

는, 텍스트를 가져옵니다 처음 500 개 문자

편집합니다 : 미안, 코드 샘플 질문을하지 읽어 보시기 바랍니다. 결정된.

var node = $('#elt').parent(); 
var text = node.text(); 

while (text.length < 500) { 
    node = node.nextSibling; 
    if (node.nodeType === 1) { 
     text += node.text(); 
    } else if (node.nodeType === 3) { 
     text += node.nodeValue; 
    } 
} 

text = text.slice(0, 500); 
+0

죄송합니다. – user615816

+0

예를 들어 20 자로 몇 가지 작은 예제를 제공 할 수 있습니까? – callumacrae

+0

내가 무슨 뜻인지, 요소를 찾아서 텍스트 'text1'을 얻는다. 다음이 요소 뒤에 오는 모든 텍스트를 가져 오십시오. 'text2','(text1 + text2) .slice (0, 500);' – user615816

관련 문제