开发者

How do I change the color of text in javascript?

开发者 https://www.devze.com 2022-12-14 16:48 出处:网络
I wrote a javascript code displaying the 开发者_如何学Cdate. How would I change the color?You need to put the text in a separate element, then change the element\'s color CSS property.

I wrote a javascript code displaying the 开发者_如何学Cdate. How would I change the color?


You need to put the text in a separate element, then change the element's color CSS property.

This is easiest to do using jQuery:

<span id="date">Please enable JavaScript</span>

$('#date').text(new Date().toString()).css('color', 'red');

However, you might want to do it with pure CSS:

(In the head tag:)

<style type="text/css">
    #date {
        color: red;
    }
</style>    


You could set inline CSS properties of the element where you display the date, by using the element.style property:

var el = document.getElementById('elementId');
el.innerHTML = date; // set the text
el.style.color = '#ff0000'; // set the text color

Or you could apply a CSS class programmatically:

el.className = 'yourclass';
0

精彩评论

暂无评论...
验证码 换一张
取 消