将框的颜色更改为事件中的随机数组元素

Change color of box to random array element on an event

您好,我编写了这段代码,以便在鼠标进入框的区域时更改框的颜色。 mouseenter 上有一个事件,我做了一个函数,它应该 return 一个数组中的随机颜色。它似乎不起作用。

$("#colorBox1Id").mouseenter(function(){
    $(this).css("background-color", SelectRandomColor) 
});


function SelectRandomColor() {
    var colorArray= new Array("blue", "red", "yellow", "green");
    var selectedColor = colorArray[Math.floor(Math.random*colorArray.length)];

    return selectedColor;
}`

希望有人能提出解决这个问题的建议。

尝试colorArray[Math.floor(Math.random()*colorArray.length)];

$("#colorBox1Id").mouseenter(function() {
  $(this).css("background-color", SelectRandomColor)
});


function SelectRandomColor() {
  var colorArray = new Array("blue", "red", "yellow", "green");
  var selectedColor = colorArray[Math.floor(Math.random() * colorArray.length)];

  return selectedColor;
}
#colorBox1Id {
  width: 100px;
  height: 100px;
  border: 1px solid #ddd;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="colorBox1Id">
  Colorbox
</div>

您忘记打电话给 random

var selectedColor = colorArray[Math.floor(Math.random()*colorArray.length)];
                                                     ^^