我一直在尝试完成一个小练习,我让它在 codepen 中运行,但是当我通过 sublime 并在浏览器中打开它时,它没有显示任何内容!我尝试通过 JSHint 运行它,并一遍又一遍地阅读它。如果有人能花点时间快速阅读一下,我会很高兴。 ..这就是它应该做的.. http://codepen.io/hellojessicagraham/pen/Ropgob
var htmlDot = "";
var red;
var green;
var blue;
var rgbColor;
function colourSelect() {
return Math.floor(Math.random() * 256 );
}
for(var i = 1; i<=100; i+=1) {
red = colourSelect();
green = colourSelect();
blue = colourSelect();
rgbColor = "rgb(" + red + "," + green + "," + blue + ")";
htmlDot += "<div style=\"background-color:"+ rgbColor + " \"></div>";
}
document.write(htmlDot);
HTML如下
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="main.css">
</head>
<body>
<script src="script.js"></script>
</body>
</html>
CSS如下
div{
width: 50px;
height: 50px;
display: inline-block;
border-radius: 50%;
margin: 5px;
}
提前谢谢
最佳答案
由于它似乎适用于您的 html 文档上的所有代码,您确定 js/css 文件的链接是正确的吗?如果您不确定,请使用导航器中的检查器 (F12)。
编辑:一切正常。就像 Dejan 所说,你的错误可能出在你的样式表中。
var htmlDot = "";
var red;
var green;
var blue;
var rgbColor;
function colourSelect() {
return Math.floor(Math.random() * 256 );
}
for(var i = 1; i<=100; i+=1) {
red = colourSelect();
green = colourSelect();
blue = colourSelect();
rgbColor = "rgb(" + red + "," + green + "," + blue + ")";
htmlDot += "<div style=\"background-color:"+ rgbColor + " \"></div>";
}
document.write(htmlDot);
button {
width:50px;
height: 50px;
border-radius: 50%;
margin: 0px;
display: inline-block;
}
div{
width: 50px;
height: 50px;
display: inline-block;
border-radius: 50%;
margin: 5px;
}
关于javascript - 控制台上没有错误,使小圆圈在刷新时改变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41528158/