"このアイテムの色はマゼンタです"または "この出力は赤です"という段落またはh1があります。色(赤、黒、シアン、マゼンタ、黄)のテキストは、段落またはh1タグのどこかにある可能性があります。段落内のテキスト値に基づいてテキストの色を変更する方法
これまでのコードです。
HTML
<div id="foo">
red magenta yellow black blue
</div>
<h1>magenta toner cartridge</h1>
CSS
.red{
color: red;
}
.magenta{
color: magenta;
}
JAVASCRIPT
$("div:contains('magenta')").each(function() {
$(this)
.html($(this)
.html()
.replace("magenta", "<span class='magenta'>magenta</span>"));
});
$("div:contains('red')").each(function() {
$(this)
.html($(this)
.html()
.replace("red", "<span class='red'>red</span>"));
});
$("h1:contains('magenta')").each(function() {
$(this)
.html($(this)
.html()
.replace("magenta", "<span class='magenta'>MAGENTA</span>"));
});
質問 マジェンタとレッドという単語の背景とテキストの色を、要素内のテキストに基づいて動的に変更するにはどうすればよいですか?
あなたは、いくつかのコードを投稿する必要があります。 –
何か試してみたら、それも投稿してください。 – yezzz
http://jsfiddle.net/5bSgc/72/ –