0

(下部の JSfiddle の失敗)

そのようなJSを考えると

// Fill the textarea for testing!
$("#input").val("hello <b>2</b> and <b>3</b>");
// Get the textarea, convert to html string, find b elements, get text content:
$("#input").keyup(function () {
    $('#output').html($("#input").val()).find("b").text();
}).keyup();

次のような HTML が与えられた場合:

<!-- INPUT: -->
<fieldset>
    <textarea id="input"></textarea>
</fieldset>

<!-- OUTPUT: -->
<b>List of text in b balises is:</b>
<div id="output">
    Here should be the list of n strings in b n balises (aka: ["2", "3"])
</div>

b要素内のn文字列のリストを取得するには?

現在、これは機能しません。 JSfiddleを参照してください。JSfiddle で答えてください。

4

3 に答える 3

1

テキストを取得して HTML として非表示の要素に配置すると、それが DOM オブジェクトに変換され、要素に移動してコンテンツを抽出できます。

于 2013-11-01T17:51:54.327 に答える
0

答え:

// Fill the textarea for testing!
$("#input").val("hello <b>2</b> and <b>3</b>");

// Get the textarea, convert to html string, find b elements, get text content:
$("#input").keyup(function () {
    $('#output').html($('#input').val());
    var list = $('#output').find("b");
    $('#output').html(""); //clean up
    list.each(function( i ) {
        $("#output").append(  "Element "+i+":" + $( this ).text() +"<br />");
    });
}).keyup();

フィドル: http://jsfiddle.net/6hCwZ/18/

于 2013-11-01T17:52:58.053 に答える