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

クラスverseを持つスパン内のテキストを抽出したいだけで、クラスtransを持つスパンからのテキストを含めるべきではありません。

結果は配列形式でなければなりません。

上記の文字列から、次のような結果を取得する必要があります

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

ありがとう

4

1 に答える 1

1

これはどう:

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();

DOM に追加しなくてもループできるはずですが、何らかの理由で機能しません。遅かったか、夕方に向けて指がストライキしているからかもしれません。いずれにせよ、誰かがそれを添付せずにそれを行う方法を見つけたら、投稿してください。私は賛成票を投じます.

于 2012-04-11T02:09:47.730 に答える