0
我有內容的可編輯GWT的DivElement的一個實例,其我動態附着某些元素。每次向父級DivElement實例添加子元素時,我可以觸發事件嗎?是否可以在GWT中向父級DivElement實例添加或刪除子元素時觸發事件?
我有內容的可編輯GWT的DivElement的一個實例,其我動態附着某些元素。每次向父級DivElement實例添加子元素時,我可以觸發事件嗎?是否可以在GWT中向父級DivElement實例添加或刪除子元素時觸發事件?
您可以使用MutationObserver做你所需要的。
您設立觀察員對你的DivElement和觀察childList
突變。在MutationRecord
中,您可以獲得addedNodes
列表。
沒有爲MutationObserver沒有原生GWT的支持,所以你需要使用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);
您的DivElement。