<body>
<div>
<button type="button" onclick="add()">添加图片</button>
<button type="button" onclick="del()">删除图片</button>
</div>
<div id='stage'>
</div>
<script type="text/javascript">
var pic;
function add(){
pic = document.createElement("img");
pic.src = "F:\enjoy\doge.png";
var stage = document.getElementById("stage");
stage.appendChild(pic);
}
function del(){
var stage = document.getElementById("stage");
stage.removeChild(pic);
}
</script>
</body>
js中重复点击添加按钮添加多个元素,删除按钮函数怎么写?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
天际的海浪 2022-07-05 22:41关注你题目的解答代码如下:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> <title> 页面名称 </title> </head> <body> <div> <button type="button" onclick="add()">添加图片</button> <button type="button" onclick="del()">删除图片</button> </div> <div id='stage'> </div> <script type="text/javascript"> var pic; function add(){ pic = document.createElement("img"); pic.src = "F:\enjoy\doge.png"; var stage = document.getElementById("stage"); stage.appendChild(pic); } function del(){ var stage = document.getElementById("stage"); stage.removeChild(stage.lastElementChild); } </script> </body> </html>如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录