2012-03-21 9 views
1

私はこのコードを持っています。JQueryは要素で始まるテキストを取得します

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タグを削除します。

<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")で始まります。

答えて

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

がテキストを取得し、

EDIT最初の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

申し訳ありませんが、私の英語では、$( '#elt')の前にテキストがあり、$( '#elt')で始まるテキストのみが必要です。 – user615816

+0

小文字の例をいくつか挙げてください。 – callumacrae

+0

私が意味することは、要素を見つけて、そのテキスト「text1」を得ることです。 そして、この要素の後ろのすべてのテキスト、 'text2'、'(text1 + text2).slice(0、500); ' – user615816

関連する問題