1

次の HTML ブロックがあります。

<div class='translate' id='community_participation'>
Supported by
<span class='notranslate'>
52
</span>
customers like
you, as well as
<a href="/gsfnmichelle/people">the <span class='notranslate'>GSFNMICHELLE</span> team</a>.
</div>

私はすべてを変えることができることを知っています$('#community_participation.translate').html('Something here.');

しかし、スパン間、つまり「Supported by」、「customers like you, as as」、「the」、および「team」の間でテキストを変更する方法がわかりません。

4

5 に答える 5

1

管理しやすいように、 jQuery contents()を使用してテキスト ノードを取得し、新しい要素内にラップすることをお勧めします。

var el = $('.translate').contents().filter(function() {
             return this.nodeType == Node.TEXT_NODE;
         }).eq(0).wrap('<span class="newDiv"></span>');

次に、text()を使用してテキストを変更できます。

$('span.newDiv').text('New Text');

フィドル

于 2013-04-22T15:56:16.767 に答える
1

たとえば、「Supported by」というテキストを変更するには、マークアップの最初の textNode を調べます (インデックス 0 を使用)。

$(function() {
    $('#community_participation').contents().filter(function() {
        return this.nodeType == 3;
    })[0].textContent = "Support comes from";
});

出力:
サポートは、GSFNMICHELLE チームだけでなく、あなたと同じ 52 人からもたらされます。

于 2013-04-22T16:02:35.870 に答える
0

変更したいセクションをスパンでラップできない場合は、おそらく置換を使用できます。例:

$('a.question-hyperlink').html($('a.question-hyperlink').html().replace('Changing','Something else'))
于 2013-04-22T15:48:24.480 に答える