更改字体颜色javascript

时间:2014-11-05 20:44:37

标签: javascript fonts

我有这段代码。

$(window).bind("scroll", function() {
    if ($(this).scrollTop() > a.top -80) {
        $(".introHeader").fadeIn();
    } else {
        $(".introHeader").stop().fadeOut();
    }

现在我想更改div中文本的字体颜色:introHeader。 所以:

$(window).bind("scroll", function() {
    if ($(this).scrollTop() > a.top -80) {
        $(".introHeader").fadeIn();
        // Change font color.
    } else {
        $(".introHeader").stop().fadeOut();

我该如何解决这个问题,

    $(window).bind("scroll", function() {
    if ($(this).scrollTop() > a.top -80) {
        $(".introHeader").fadeIn();
        document.getElementById('introHeader').color = '#999999';
    } else {
        $(".introHeader").stop().fadeOut();

不起作用..

2 个答案:

答案 0 :(得分:1)

这应该有效

    if ($(this).scrollTop() > a.top -80) {
        $(".introHeader").fadeIn();
        // Change font color.
        $(".introHeader").attr('style', 'color: #999999;');
    } else {
       ...
    }

应该注意的是,有更好的方法可以做到这一点。使用CSS类更好地优化子选择器,然后使用.addClass('className')

答案 1 :(得分:1)

$(".introHeader").css('color', '#999').fadeIn();

你已经在使用jquery了。也可以使用css方法。

并且您的样本无法正常工作,因为颜色不是属性。您必须使用style.color

document.getElementById('introHeader').style.color = '#999999';