1

私はこのコードを持っています:

<div class="bodytext1typeenc">Ce qu’il faut retenir
    <div class="al">Budget «solidarités».</div>
</div>

「Ce qu'il faut retenir」だけを入手したい。私はこれを試しました:

$('.bodytext1typeenc').text() // --> doesn't work.
$('.bodytext1typeenc').remove('.al') //  --> doesn't work.

何か助けてください?ありがとう !

4

3 に答える 3

5

クローンを作成し、子を削除して、テキストを取得できます。下記参照、

var $clone = $('.bodytext1typeenc').clone();
$clone.children().remove()
$clone.text(); //should return the div's text

注:元のコンテンツを保持したくない場合は、複製する必要はありません。

デモ: http://jsfiddle.net/PX2yA/

于 2013-10-18T14:12:16.780 に答える
1

代わりにこれを試してください。あなたがしたいことは、要素を複製してから子要素を削除することです

http://jsfiddle.net/HgdyG/

$(".bodytext1typeenc")
        .clone()    //clone the element
        .children() //select all the children
        .remove()   //remove all the children
        .end()  //again go back to selected element
        .text();

これを頻繁に使用する場合は、このような単純な拡張機能を作成できます

jQuery.fn.noChild= function() {

    return $(this).clone()
            .children()
            .remove()
            .end()
            .text();

};

次に実行します

$(".bodytext1typeenc").noChild();
于 2013-10-18T14:13:40.853 に答える
0

contents() を使用して、noteType TEXT_NODE を除外することができます (3)

var val = $('.bodytext1typeenc').contents().filter(function() {
  return this.nodeType == 3;
}).text();
val = $.trim(val);

alert('"' + val + '"'); // "Ce qu’il faut retenir"

デモ: http://jsbin.com/AsilIpo/1/

于 2013-10-18T14:22:31.200 に答える