Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过单击链接生成随机背景色_Javascript_Html_Css - Fatal编程技术网

Javascript 通过单击链接生成随机背景色

Javascript 通过单击链接生成随机背景色,javascript,html,css,Javascript,Html,Css,在我的JavaScript程序中,我使用Math.random()随机生成了背景颜色。加载页面后,它会自动生成随机颜色。我也创建了链接点击这里。当用户点击链接时,它也应该为我使用的Onclick函数生成背景色,并添加了随机生成颜色的代码,但当我点击链接时,它不会生成背景色。每次我点击链接,它都会产生随机的颜色。有人能纠正我吗 代码: #帕拉{ 文本对齐:居中; 字体大小:20px; 颜色:白色; } #链接{ 文本对齐:居中; 字体大小:20px; } lab13 文件。写(“”);文件。写(

在我的JavaScript程序中,我使用Math.random()随机生成了背景颜色。加载页面后,它会自动生成随机颜色。我也创建了链接点击这里。当用户点击链接时,它也应该为我使用的Onclick函数生成背景色,并添加了随机生成颜色的代码,但当我点击链接时,它不会生成背景色。每次我点击链接,它都会产生随机的颜色。有人能纠正我吗

代码:


#帕拉{
文本对齐:居中;
字体大小:20px;
颜色:白色;
}
#链接{
文本对齐:居中;
字体大小:20px;
}
lab13
文件。写(“
”);文件。写(“
”); document.write(“

”+”背景色是随机生成的“+”

”); 函数random_bg_color(){ RGB颜色变量; 红色=数学地板(数学随机()*250+0); 绿色=Math.floor(Math.random()*250+0); 蓝色=Math.floor(Math.random()*250+0); rgb颜色='rgb('+红色+'、'+绿色+'、'+蓝色+)'; document.body.style.background=rgbColor; 文件。写入(“

RGB(“+red+”、“+green+”、“+blue+”)

”); 红色=(“0”+red.toString(16)).substr(-2).toUpperCase(); 绿色=(“0”+绿色.toString(16)).substr(-2).toUpperCase(); blue=(“0”+blue.toString(16)).substr(-2).toUpperCase(); 文档。写(“

HEX:#“+红色+”+绿色+”+蓝色+”

”; } 随机_bg_color(); 函数randomize(){ 随机_bg_color(); document.body.style.background=rgbColor; }
输出:


问题在于,
randomize()
只是设置背景色,而不是实际生成新颜色。因此,下面的
randomize()
将生成一种新颜色,并将背景色设置为该值

它将在页面加载时被调用(最后一行),链接上的
onclick
将直接调用它

function randomize() {
  var rgbcolor;
  red = Math.floor(Math.random() * 250 + 0);
  green = Math.floor(Math.random() * 250 + 0);
  blue = Math.floor(Math.random() * 250 + 0);

  rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';
  document.body.style.background = rgbColor;

  red = ("0" + red.toString(16)).substr(-2).toUpperCase();
  green = ("0" + green.toString(16)).substr(-2).toUpperCase();
  blue = ("0" + blue.toString(16)).substr(-2).toUpperCase();
}

randomize();

问题是,
randomize()
只是设置背景色,而不是实际生成新颜色。因此,下面的
randomize()
将生成一种新颜色,并将背景色设置为该值

它将在页面加载时被调用(最后一行),链接上的
onclick
将直接调用它

function randomize() {
  var rgbcolor;
  red = Math.floor(Math.random() * 250 + 0);
  green = Math.floor(Math.random() * 250 + 0);
  blue = Math.floor(Math.random() * 250 + 0);

  rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';
  document.body.style.background = rgbColor;

  red = ("0" + red.toString(16)).substr(-2).toUpperCase();
  green = ("0" + green.toString(16)).substr(-2).toUpperCase();
  blue = ("0" + blue.toString(16)).substr(-2).toUpperCase();
}

randomize();

只需要将函数调用放在正确的位置

function randomize() {
  random_bg_color();
  document.body.style.background = rgbColor;
}

只需要将函数调用放在正确的位置

function randomize() {
  random_bg_color();
  document.body.style.background = rgbColor;
}

您应该调用
random_bg_color()
内部
随机
函数,以便每次重新生成新颜色。

您应该调用
随机_bg_color()内部
随机
功能,以便每次重新生成新颜色。

函数getRandomColor(){ 变量字母='0123456789ABCDEF'; var color='#'; 对于(变量i=0;i<6;i++){ 颜色+=字母[Math.floor(Math.random()*16)]; } 返回颜色; }; 函数随机化(){ document.body.style.background=getRandomColor(); };
函数getRandomColor(){ 变量字母='0123456789ABCDEF'; var color='#'; 对于(变量i=0;i<6;i++){ 颜色+=字母[Math.floor(Math.random()*16)]; } 返回颜色; }; 函数随机化(){ document.body.style.background=getRandomColor(); };
只需在onclick中调用random\u bg\u color()

function random\u bg\u color(){
RGB颜色变量;
红色=数学地板(数学随机()*250+0);
绿色=Math.floor(Math.random()*250+0);
蓝色=Math.floor(Math.random()*250+0);
rgb颜色='rgb('+红色+'、'+绿色+'、'+蓝色+)';
document.body.style.background=rgbColor;
红色=(“0”+red.toString(16)).substr(-2).toUpperCase();
绿色=(“0”+绿色.toString(16)).substr(-2).toUpperCase();
blue=(“0”+blue.toString(16)).substr(-2).toUpperCase();
}
随机_bg_color()

#帕拉{
文本对齐:居中;
字体大小:20px;
颜色:白色;
}
#链接{
文本对齐:居中;
字体大小:20px;
}
lab13
只需在onclick中调用random\u bg\u color()

function random\u bg\u color(){
RGB颜色变量;
红色=数学地板(数学随机()*250+0);
绿色=Math.floor(Math.random()*250+0);
蓝色=Math.floor(Math.random()*250+0);
rgb颜色='rgb('+红色+'、'+绿色+'、'+蓝色+)';
document.body.style.background=rgbColor;
红色=(“0”+red.toString(16)).substr(-2).toUpperCase();
绿色=(“0”+绿色.toString(16)).substr(-2).toUpperCase();
blue=(“0”+blue.toString(16)).substr(-2).toUpperCase();
}
随机_bg_color()

#帕拉{
文本对齐:居中;
字体大小:20px;
颜色:白色;
}
#链接{
文本对齐:居中;
字体大小:20px;
}
lab13

问题在于您调用的函数不正确

单击后需要调用函数:random\u bg\u color

我修复了你的代码片段

function random\u bg\u color(){
RGB颜色变量;
红色=数学地板(数学随机()*250+0);
绿色=Math.floor(Math.random()*250+0);
蓝色=Math.floor(Math.random()*250+0);
rgb颜色='rgb('+红色+'、'+绿色+'、'+蓝色+)';
document.body.style.background=rgbColor;
红色=(“0”+red.toString(16)).substr(-2).toUpperCase();
绿色=(“0”+绿色.toString(16)).substr(-2).toUpperCase();
blue=(“0”+blue.toString(16)).substr(-2).toUpperCase();
}
随机_bg_color()
#第{
文本对齐:ce