我有一个内容可编辑的GWT分开的实例,我正在动态附加一些元素。我可以每次在父母分配实例中添加子元素时发射事件吗?
您可以使用MutationObserver做您需要的事情。
您在分离上设置了一个观察者,并观察childList
突变。在MutationRecord
中,您可以获取addedNodes
列表。
没有本机GWT支持突变observer,因此您需要使用jsni:
private native void addListener(Element elem) /*-{
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if(mutation.type == 'childList') {
// mutation.addedNodes contains nodeList of added nodes
$wnd.alert('Nodes added');
}
});
});
// configuration of the observer:
var config = {
childList : true
};
// pass in the target node, as well as the observer options
observer.observe(elem, config);
}-*/;
只需在您的分配上调用addListener(div);
。