2017-08-06 4 views

答えて

0

要素のスタイルプロパティを使用して、背景色とテキストの色を変更できます。下記のスニペットを参照してください。

var text="hello world"; 
 
var message = document.getElementById('message'); 
 
message.innerText = text; 
 
message.style.backgroundColor = "RED";; 
 
message.style.color = "white";
<span id="message"></span>

0

あなたはDOMノードのstyleプロパティを確認してください。それは言った、あなたはbackgroundColorcolorを探しています:

message.style.backgroundColor = 'some valid color value'; 
message.style.color = 'some valid color value'; 
+0

が、それは動作しますが、ロブは素晴らしいありがとうちょうど私が欲しい方法!申し訳ありませんが、同じ方法でフォントを変更できますか? –

+0

問題はありません。はい - message.style.fontFamily = "Arial";例えば。私のポストのリンクは利用可能なプロパティを見つけるのに役立ちます。乾杯 –

0

このコードを使用:

var message = document.getElementById("#message"); 
var text = "hello world"; 
message.innerText = text; 
message.style.backgroundColor = "#ff0000"; 
message.style.color = "#ffffff"; 

例:

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<style> 
 
#message { 
 
    width: 300px; 
 
    height: 300px; 
 
    background-color: coral; 
 
    color: white; 
 
} 
 
</style> 
 
</head> 
 
<body> 
 

 
<button onclick="myFunction()">Change Style</button> 
 

 
<br /><br /> 
 

 
<div id="message"> 
 
    Hello 
 
</div> 
 

 
<script> 
 
function myFunction() { 
 
    var message = document.getElementById("message"); 
 
\t var text = "hello world"; 
 
\t message.innerText = text; 
 
\t message.style.backgroundColor = "#ff0000"; 
 
\t message.style.color = "#ffffff"; 
 
} 
 
</script> 
 

 
</body> 
 
</html>

関連する問題