2013-11-04 38 views
1

我需要赶上$ .append()方法,像这样的jQuery:追加,前插,HTML事件

$('div').bind('append', function(){ 
    /* code here */ 
}); 

$('div').bind('html', function(){ 
    /* code here */ 
}); 

我怎样才能得到呢?

+0

你有任何更多的环境? –

+0

在追加代码 – Johan

+0

的函数中捕捉它。我需要替换我的网站上的所有选择元素。但是我的代码中有一些append()方法。 我决定赶上所有这些方法并实时更新假选择,以避免一些错误可能大概是 –

回答

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('.'); 
}); 

http://jsfiddle.net/VTmk5/1/

+0

谢谢。但我需要查看我的所有代码。如果是这样,我不需要这个功能。我需要一些通用的解决方案 –

+0

@КонстантинДаруткин您支持哪些浏览器? – Johan

+0

全部。我只看到一种解决方案 - 编辑jquery核心。但这是一种极端的情况@Johan –