效果图
随机
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="button" value="新增元素" id="btn_1">
</body>
<script>
var button=document.querySelector("#btn_1");
button.onclick=function(){
addElement();
changeColor();
}
function addElement(){
var newbox=document.createElement("img");
newbox.style.position="absolute";
newbox.style.left=Math.random()*1000+"px";
newbox.style.top=Math.random()*1000+"px";
newbox.src="./tongx.jpg";
var box=document.body;
box.appendChild(newbox);
}
function changeColor(){
var body=document.body;
var R=Math.random()*255;
var G=Math.random()*255;
var B=Math.random()*255;
body.style.backgroundColor="rgb("+R+","+G+","+B+")";
}
</script>
</html>