Skip to content

Instantly share code, notes, and snippets.

@6174
Last active December 20, 2015 17:48
Show Gist options
  • Save 6174/6171106 to your computer and use it in GitHub Desktop.
Save 6174/6171106 to your computer and use it in GitHub Desktop.
dom change event
//http://stackoverflow.com/questions/1449666/create-a-jquery-special-event-for-content-changed
(function(){
var interval;
jQuery.event.special.contentchange = {
setup: function(){
var self = this,
$this = $(this),
$originalContent = $this.text();
interval = setInterval(function(){
if($originalContent != $this.text()) {
$originalContent = $this.text();
jQuery.event.handle.call(self, {type:'contentchange'});
}
},100);
},
teardown: function(){
clearInterval(interval);
}
};
})();
jQuery.fn.watch = function( id, fn ) {
return this.each(function(){
var self = this;
var oldVal = self[id];
$(self).data(
'watch_timer',
setInterval(function(){
if (self[id] !== oldVal) {
fn.call(self, id, oldVal, self[id]);
oldVal = self[id];
}
}, 100)
);
});
return self;
};
jQuery.fn.unwatch = function( id ) {
return this.each(function(){
clearInterval( $(this).data('watch_timer') );
});
};
jQuery.fn.valuechange = function(fn) {
return this.bind('valuechange', fn);
};
jQuery.event.special.valuechange = {
setup: function() {
jQuery(this).watch('value', function(){
jQuery.event.handle.call(this, {type:'valuechange'});
});
},
teardown: function() {
jQuery(this).unwatch('value');
}
};
var title = $("b.facility");
var title = $('#title');
//method two
//the element I want to monitor
title.bind('DOMNodeInserted', function(e) {
alert('element now contains: ' + $(e.target).html());
});
//method three
//https://developer.mozilla.org/zh-CN/docs/DOM/MutationObserver
var BrowserMutationObserver = window.MutationObserver || window.WebKitMutationObserver;
@6174
Copy link
Author

6174 commented Aug 7, 2013

为了兼容ie6, 看来只能setInterval了!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment