使用到的api -- MutationObserver
MutationObserver - Web API 接口参考 | MDN
基本html代码
<body>
<button onclick="addElement()">添加元素</button>
<button onclick="deleteElement()">删除元素</button>
<div id="father" style="width: 300px;height: 300px;background-color: red;"></div>
</body>
<script>
let father = document.getElementById('father');
function addElement() {
father.innerHTML += '<div id="son" style="width: 100px;height: 100px;background-color: blue;">我是子元素</div>';
}
function deleteElement() {
father.removeChild(father.lastChild);
}
</sc