<!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>删除时弹出确认对话框</title>
</head>
<body>
<h2>删除时弹出确认对话框</h2>
<div id='deleteElement' style="height: 100px;background-color: #ccc;">将要被删除的元素</div>
<input type="button" value="删除" id="deleteConfirmation" data-msg="警告,您真的要删除上边的元素吗?"/>
<script type="text/javascript">
window.onload = function () {
document.getElementById("deleteConfirmation").onclick = function () {
if (document.getElementById("deleteElement")) {
if (window.confirm(this.getAttribute("data-msg"))) {
document.body.removeChild(document.getElementById("deleteElement"));
alert("删除成功");
}
} else {
alert("元素已经删除啦!")
}
}
}
</script>
</body>
</html>
效果: