the following is the code I wrote when I was learning, but as soon as I ran the button event in the web page JS, it ran automatically. I don"t know where I made a mistake. I looked on Baidu and saw that the button event was written like this. Please help me and see what"s wrong with my brother. Thank you.
Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="main.js"></script>
<style>
div{
width: 200px;
height: 200px;
background: -sharp998820;
}
</style>
<script>
window.onload = function(){
var oBut_1=document.getElementById("obut_1");
var oBut_2 = document.getElementById("obut_2");
var oBut_3 = document.getElementById("obut_3");
function Gored(odiv_qu, property, price, shebiao){
var oDiv = document.getElementById(odiv_qu);
oDiv.style.background = shebiao;
oDiv.style[property] =price;
}
alert(oBut_1);
oBut_1.onclick = alert("sdsd");
oBut_1.onclick=Gored("oDiv_s", "width", "25rem", "green");
oBut_2.onclick=Gored("oDiv_s", "height", "15rem", "-sharp003320");
oBut_3.onclick=Gored("oDiv_s"," ", " ", "-sharp022545");
}
</script>
</head>
<body>
<input id="obut_1" type="button" value="" />
<input id="obut_2" type="button" value="" />
<input id="obut_3" type="button" value="" />
<div id="oDiv_s"></div>
</body>
</html>