使用Javascript更新CSS中的文本颜色

时间:2014-04-08 21:07:31

标签: javascript html css

我的问题是我无法使用Javascript更新。 我想将的文字颜色从蓝色更改为 gree

风格:

<style type="text/css">
      #grid {
        background-color: #ccc0b3;
        width: 400px;
        height: 400px;
        position: relative;
        border-radius: 5px;
      }

      .box {
        width: 80px;
        height: 80px;
        border-radius: 5px;
        font-family: Arial, sans-serif;
        font-size: 35px;
        font-weight: bold;
        display: inline-block;
        position: absolute;
        padding: 5px;
        margin: 5px;
        text-align: center;
        line-height: 80px;
      }
    </style>

JavaScript:创建名为 box

的类
function makeNew(row, col) {
      var number = Math.random() < 0.9 ? 2 : 4;
      var color = pikColor(number);
      var textcolor = textColor(number);

      return grid[row][col] = $('<div>')
        .css({
          background: color,
          color: "blue",
          top  : row * 100 + 'px',
          left : col * 100 + 'px'
        })
        .text(number)
        .addClass('box')
        .appendTo($('#grid'));
    }

将文字颜色更新为格力:

function merge(row1, col1, row2, col2) {
      if (merging[row2][col2]) {
        return false;
      }

      grid[row2][col2].remove();
      grid[row2][col2] = grid[row1][col1];
      grid[row1][col1] = null;
      var number = grid[row2][col2].text() * 2 ;
      var color = pikColor(number);
      var textcolor = textColor(number);
      alert(number);
      alert(textcolor);
      merging[row2][col2] = true;

      grid[row2][col2]
        .css({
          background: color,
          color: textColor,
          top  : row2 * 100 + 'px',
          left : col2 * 100 + 'px'
        })
        .text(number);

      return true;
    }

这里是 textColor 功能

function textColor(number){
    var textColor ;
     if(number<=4){
     textColor="#776E65";
     }
     else{
     textColor="#F9F6F2";
     }
     return textColor;
    }

1 个答案:

答案 0 :(得分:0)

textColor 更改为 textcolor

color: textColor,

color: textcolor,