我需要捕捉$ .append()方法,就像这样
$('div').bind('append', function(){
/* code here */
});
或
$('div').bind('html', function(){
/* code here */
});
我怎么能得到这个?
答案 0 :(得分:0)
在你追加东西的所有地方,触发一个自定义事件:
$('#bar').append('...');
$.event.trigger('customappend');
...并在你追加代码的所有地方触发它:
$('#foo').append('...');
$.event.trigger('customappend');
在一个地方收听它:
$(document).on('customappend', function(){
/* code here */
});
解决方案#2(使用MutationObserver):
// select the target node
var target = document.querySelector('#id');
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log('target mutated');
});
});
// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true };
// pass in the target node, as well as the observer options
observer.observe(target, config);
// later, you can stop observing
// observer.disconnect();
$(document).click(function(){
$('#id').append('.');
});