[英]javascript button doesn't wait for to be clicked
I have created a simple random color generator application. 我创建了一个简单的随机颜色生成器应用程序。 The barrier is that the button created through javascript is clicked automatically even before any click event on it.
障碍是通过javascript创建的按钮甚至在其上发生任何click事件之前都会被自动单击。 I am new to javascript Here is my whole code.
我是javascript新手,这是我的整个代码。 Thanks in advance.
提前致谢。
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
function btn(){
var cgen = document.createElement('button');
cgen.innerHTML = 'Generate';
document.body.appendChild(cgen);
cgen.style.width = '100%';
cgen.style.fontSize = '20px';
cgen.style.border = '2px solid red';
cgen.onclick = paintDivs();
// cgen.setAttribute("onclick", paintDivs());
}
function genColor(){
var randomColor = "#" + Math.floor(Math.random()*16777215).toString(16);
return randomColor;
}
function paintDivs(){
for (var i = 0; i < 500; i++) {
var fColor = genColor();
var bColor = genColor();
var div = document.createElement('div');
div.innerHTML = 'Text ' + fColor + ' Back ' + bColor;
div.style.color = fColor;
div.style.background = bColor;
div.style.width = '24%';
div.style.float = 'left';
div.style.margin = '5px';
div.style.height = '60px';
div.style.fontSize = '18px';
div.style.lineHeight = '60px';
div.style.textAlign = 'center';
div.style.borderRadius = '20px';
document.body.appendChild(div);
}
}
</script>
</head>
<body onload="btn();" style="background-color: gray;">
</body>
</html>
</html>
cgen.onclick = paintDivs();
That calls paintDivs()
immediately and sets cgen.onclick
to its return value. 这将立即调用
paintDivs()
并将cgen.onclick
设置为其返回值。 What you want is this: 您想要的是:
cgen.onclick = paintDivs;
try below code . 尝试下面的代码。 have change cgen.onclick = paintDivs();
有变化cgen.onclick = paintDivs(); to cgen.onclick = paintDivs;
到cgen.onclick = paintDivs;
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
function btn(){
var cgen = document.createElement('button');
cgen.innerHTML = 'Generate';
document.body.appendChild(cgen);
cgen.style.width = '100%';
cgen.style.fontSize = '20px';
cgen.style.border = '2px solid red';
cgen.onclick = paintDivs;
// cgen.setAttribute("onclick", paintDivs());
}
function genColor(){
var randomColor = "#" + Math.floor(Math.random()*16777215).toString(16);
return randomColor;
}
function paintDivs(){
for (var i = 0; i < 500; i++) {
var fColor = genColor();
var bColor = genColor();
var div = document.createElement('div');
div.innerHTML = 'Text ' + fColor + ' Back ' + bColor;
div.style.color = fColor;
div.style.background = bColor;
div.style.width = '24%';
div.style.float = 'left';
div.style.margin = '5px';
div.style.height = '60px';
div.style.fontSize = '18px';
div.style.lineHeight = '60px';
div.style.textAlign = 'center';
div.style.borderRadius = '20px';
document.body.appendChild(div);
}
}
</script>
</head>
<body onload="btn();" style="background-color: gray;">
</body>
</html>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.