2 回答

TA貢獻(xiàn)1804條經(jīng)驗 獲得超2個贊
沒有返回任何內(nèi)容,因此修復(fù)它的GetValue()一種方法是返回兩種顏色,然后按如下所示分配它們:
function GetValue() {
var myarray = new Array("#ff0000", "#ffe100", "#95ff00", "#2c8d94", "#ad6428", "#28ad9d");
var randomColor1 = myarray.splice(
Math.floor(Math.random() * myarray.length), 1)[0];
var randomColor2 = myarray.splice(
Math.floor(Math.random() * myarray.length), 1)[0];
/* Return like this*/
return [randomColor1, randomColor2];
}
并將 changeBG() 更改為
function changeBg() {
/* Get colors like this since they are returned like this*/
var [randomColor1, randomColor2] = GetValue();
/* Log it to see what colors they are*/
console.log(randomColor1, randomColor2);
/* Call the apply method*/
applyChanges(randomColor1, randomColor2);
}

TA貢獻(xiàn)1804條經(jīng)驗 獲得超3個贊
您必須簡化您的 GetValue 函數(shù):只需創(chuàng)建 1 個值并返回它。其余的都可以。
讓我們在這里試試:
function GetValue() {
var myarray = new Array( "#ff0000", "#ffe100", "#95ff00", "#2c8d94", "#ad6428", "#28ad9d");
return myarray.splice(Math.floor(Math.random() * myarray.length),1)[0];
}
var styles = ["to right", "to bottom right", "-90deg"];
function applyChanges(randomColor1, randomColor2) {
var randomColor1 = GetValue();
var randomColor2 = GetValue();
var bg = "";
var style = Math.floor(Math.random() * styles.length);
bg = "linear-gradient(" + styles[style] + "," + randomColor1 + "," + randomColor2 + ")";
$("body").css("background", bg);
$("#myInput").text(bg);
}
function changeBg() {
var randomColor1 = GetValue();
var randomColor2 = GetValue();
applyChanges(randomColor1, randomColor2);
}
document.getElementById('btn').addEventListener('click', function() {
changeBg();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='message'></div>
<input id='myInput'>
<button id='btn'>Click me</button>
添加回答
舉報