JavaScript教程
首页 > JavaScript教程 > JavaScript实现页面动态添加图片

JavaScript实现页面动态添加图片

2020-09-10 10:58:38 56 0

编辑 收藏

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<div id="Banner">点击我</div>
	<script>
		window.onload = function(){
			document.body.addEventListener('click', function () {
     			 var myParent = document.getElementById("Banner"); 
     			 var myImage = document.createElement("img");
			     myImage.src = 'https://thecatapi.com/api/images/get?format=src&type=gif';
			     myParent.appendChild(myImage);
			     myImage.style.marginLeft = "160px";
			});
		}
	</script>
</body>
</html>

取消回复欢迎 发表评论: