2012-10-17 81 views
2

可能重复:
Is there a jQuery DOM change listener?DOM的修改

有办法有一个js函数加载或每次当DOM被修改时调用该函数?我有一个html页面,我在这个页面上从动作按钮中展开另一个面板,这样dom被修改,我的js函数不再被调用:

+0

http://stackoverflow.com/questions/1091661/detect-element-content-changes-with-jquery http://stackoverflow.com/questions/2844565/is-there-a-jquery-dom-change-listener – bhb

+0

不,但您可以创建一个自定义事件并在每次完成修改时触发它DOM。 –

+0

以及如何做到这一点? – mcmwhfy

回答

3

代码

$("#a").bind("DOMSubtreeModified", function() { 
    if (window.PIE) { 
     $(this).each(function() { 
      PIE.attach(this); 
     }); 
    } 
    alert("tree changed"); 
}); 


$("#add").click(function() { 
    $("#a").append("<span>hey there</span>"); 
}); 
​ 

HTML:

<div id="a"></div> 
<button id="add">Add to Div</button>​ 

工作fiddle

+0

非常感谢..! – mcmwhfy