---css
<style type="text/css">
img{width: 10px;height: 10px;background-color: green;}</style>
--html
<body>
<h1>标题</h1>
<div id='div1'></div>
<button onclick="createImg()">添加图片</button>
</body>
---js
<script type="text/javascript">
function createImg(){
//1.创建img节点
var imgNode=document.createElement('img');
var div1=document.getElementById('div1');
div1.appendChild(imgNode);
}
</script>
这篇博客介绍了如何在HTML中利用JavaScript动态生成img标签。通过创建img节点,并将其添加到指定的div元素内,实现页面上图片的动态插入。点击按钮即可触发添加图片的功能。
1049

被折叠的 条评论
为什么被折叠?



