【发布时间】:2014-01-29 02:24:10
【问题描述】:
我想知道对于给定的MutationRecord,在Elements 周围找到最干净的方法是什么?
MutationRecord 有.nextSibling、.previousSibling 属性,但可能包含文本节点,我需要获取常规元素节点。
工作示例在这里:http://jsfiddle.net/tomalec/ceku2/
我尝试过使用
mutation.addedNodes[0].previousElementSibling;
mutation.addedNodes[mutation.addedNodes.length - 1].nextElementSibling);
http://jsfiddle.net/tomalec/Q2HFY/2/
不过看起来有点冒险,而且对于被移除的完全不起作用。
mutation.removedNodes[0].previousElementSibling; //null
mutation.removedNodes[mutation.removedNodes.length - 1].nextElementSibling); //null
【问题讨论】:
标签: javascript dom mutation-observers