最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS: message.innerText font color and bg color - Stack Overflow

programmeradmin0浏览0评论

I'm using message.innerText for string output in google extension.

var text="hello world";
message.innerText = text;

Question is how to make bg color and text color different? Thank you!

I'm using message.innerText for string output in google extension.

var text="hello world";
message.innerText = text;

Question is how to make bg color and text color different? Thank you!

Share Improve this question asked Aug 6, 2017 at 18:53 Jessica RayJessica Ray 652 silver badges5 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

You should review the style property of DOM nodes. That said, you are looking for backgroundColor and color:

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

Use this code:

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

Example:

<!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");
	var text = "hello world";
	message.innerText = text;
	message.style.backgroundColor = "#ff0000";
	message.style.color = "#ffffff";
}
</script>

</body>
</html>

you can change the background color and text color by using the style properties of elements. see below snippet.

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)

  1. 暂无评论