2012-04-11 9 views
0
html_string "<span class=\"verse\"><strong>1<\/strong>\u00a0hello world how are you?,<span class=\"trans\" title=\"\u00a0Greek brothers.\">t<\/span> I am fine thank you.<\/span><span class=\"verse\"><strong>2<\/strong>\u00a0this world is very bad.<\/span><span class=\"verse\"><strong>3<\/strong>\u00aall me are good,<\/span>" 

Ich will nur den Text innerhalb der Spanne extrahieren, die Klasse Vers hat und es soll nicht Text von Spannweite mit Klasse trans umfasst.Extrakt Text aus HTML-String mit jquery

Das Ergebnis muss in Array-Form vorliegen.

von oben Zeichenfolge muss ich so

["\u00a0hello world how are you? I am fine thank you","\u00a0this world is very bad.","\u00aall me are good,"] 

Dank

Antwort

1

Wie über dieses Ergebnis:

var html_string = "<span class=\"verse\">" 
        + "<strong>1</strong>\u00a0hello world how are you?," 
        + "<span class=\"trans\" title=\"\u00a0Greek brothers.\">t<\/span> " 
        + "I am fine thank you." 
       + "</span>" 
       + "<span class=\"verse\">" 
        + "<strong>2</strong>\u00a0this world is very bad." 
       + "</span>" 
       + "<span class=\"verse\">" 
        + "<strong>3<\/strong>\u00aall me are good," 
       + "</span>"; 

// Build up the DOM in a hidden element we can parse through 
var $html = $('<div>',{html:html_string}).hide().appendTo('body'); 

// loop through each verse  
$html.find('.verse').each(function(i,e){ 
    // grab the verse, but first delete any span.trans 
    var verse = $(e).find('span.trans').remove().end().text(); 
    // console.log(verse); 
}); 
// remove the html from the DOM 
$html.remove(); 

Sie sollten es zu Schleife in der Lage sein, ohne dass es auf das DOM hinzufügen, aber aus irgendeinem Grund kann ich es nicht zur Arbeit bringen. Vielleicht weil es zu spät ist oder meine Finger streiken für den Abend. Wie auch immer, wenn jemand einen Weg findet, es zu tun, ohne es anzuhängen, bitte posten Sie und ich werde abstimmen.

+0

Und die obligatorische Demo: http://jsfiddle.net/rWYzb/ –