JavaScript实现页面动态添加图片

<!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>

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

微信扫一扫

微信扫一扫

微信扫一扫,分享到朋友圈

JavaScript实现页面动态添加图片