直接看代码,msgList 是一个 ul 元素,target.parentNode 是 ul 下的 li,target 是 li 内部的 a 标签,要实现的功能是在 ul 上设置了 click 的事件监听,当点击 a 的时候,通过事件冒泡,会删除包含该 a 标签的 li 标签。但是如下的写法最终该元素也删除了,但是控制台却报了一个错误,没想明白怎么回事,请大神指教一二。
MDN上有针对removeChild上可能造成的异常有解释:The method throws an exception in 2 different ways:If the child was in fact a child of element and so existing on theDOM, but was removed the method throws the following exception:Uncaught NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.If the child doesn't exist on the DOM of the page, the method throwsthe following exception:Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'.If child was in fact a child of element at the time of the call, but was removed by an event handler invoked in the course of trying to remove the element (eg, blur.)