3

私はouterHTMLを変更したいのですが、これが私がしていることです:

$(data.description)[0].outerHTML = $(data.description)[0].outerHTML.replace('red', 'black');

値は次のとおりです。

$(data.description)[0].outerHTML: "<p style="color: red;">desc1</p>"

そしてそれは変わりません。変更方法は?

data.description には次の値があります。

<p style="color: red;">desc1</p><p style="color: red;">desc2</p>

そして、 $(data.description)[0] だけを変更したい。

これが私のコード全体です:

var ingsLines = $(data.description);
for (var i =0; i < ingsLines.length; i++) {
    if (someCondition) {
        $(data.description).eq(i).css('color', 'black');
    }
}
try{$('#myTextarea').html(data.description);}catch(err){}

コードは値を変更し、赤の代わりに黒を置きますが、data.description は赤のままです。

4

2 に答える 2

1

使用する

$(data.description).replaceWith(function(){
    return this.outerHTML.replace('red', 'black')
});

デモンストレーション

最初のものだけを変更したい場合は、これを使用できます:

$(data.description).eq(0).replaceWith(function(){
    return this.outerHTML.replace('red', 'black')
});
于 2012-10-30T10:09:16.170 に答える
1

css最初の要素の色のプロパティを変更する場合は、メソッドとeq メソッドを使用できます。

$(data.description).eq(0).css('color', 'black');
于 2012-10-30T10:09:19.633 に答える