Google拡張機能の文字列出力にmessage.innerTextを使用しています。JS:message.innerTextフォントの色とbgの色
var text="hello world";
message.innerText = text;
質問どのようにbgの色とテキストの色を異ならせるのですか? ありがとうございました!
Google拡張機能の文字列出力にmessage.innerTextを使用しています。JS:message.innerTextフォントの色とbgの色
var text="hello world";
message.innerText = text;
質問どのようにbgの色とテキストの色を異ならせるのですか? ありがとうございました!
要素のスタイルプロパティを使用して、背景色とテキストの色を変更できます。下記のスニペットを参照してください。
var text="hello world";
var message = document.getElementById('message');
message.innerText = text;
message.style.backgroundColor = "RED";;
message.style.color = "white";
<span id="message"></span>
あなたはDOMノードのstyle
プロパティを確認してください。それは言った、あなたはbackgroundColor
とcolor
を探しています:
message.style.backgroundColor = 'some valid color value';
message.style.color = 'some valid color value';
このコードを使用:
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>
が、それは動作しますが、ロブは素晴らしいありがとうちょうど私が欲しい方法!申し訳ありませんが、同じ方法でフォントを変更できますか? –
問題はありません。はい - message.style.fontFamily = "Arial";例えば。私のポストのリンクは利用可能なプロパティを見つけるのに役立ちます。乾杯 –