函数的正确语法返回以更改Javascript中的div维度

时间:2016-09-09 21:52:39

标签: javascript html css

当我通过使用函数的返回单击它时,我试图使div的高度和宽度发生变化,但我似乎无法弄清楚如何让语法读取正确的(ei。" 400px")。在此先感谢您的帮助!

document.getElementById("box").onclick = function() {
  function color() {
    var choice = ["red", "blue", "green", "yellow", "purple", "orange", "pink", "gray", "brown"];
    var random = Math.random();
    random = (11 * random) / 1.25;
    random = Math.floor(random);
    return choice[random];
  }

  function size() {
    var dimensions = x + 'px';
    var x = Math.random();
    x = x * 1000;
    x = Math.floor(x);
    return dimensions;
  }

  clickedTime = Date.now();
  reactionTime = (clickedTime - createdTime) / 1000;
  document.getElementById("time").innerHTML = reactionTime;
  this.style.display = "none";
  this.style.background = color();
  this.style.height = size();
  this.style.height = size();
  size();

  boxReset();
}

boxReset();

1 个答案:

答案 0 :(得分:1)

除了某种形式的function声明之外,代码按其出现的顺序处理。

function size() {
  // x is undefined at this point and mostly useless
  var x = Math.random();
  // x is useful now
  x = x * 1000;
  // x is more useful
  x = Math.floor(x);
  // x is super useful now!
  var dimensions = x + 'px';
  // dimensions is useful now, too
  return dimensions;
}